Passing value from model's property to #Html.RadioButtonFor() [duplicate] - razor

I have a HTML table as below in my View:
<table id="tblCurrentYear">
<tr>
<td>Leave Type</td>
<td>Leave Taken</td>
<td>Leave Balance</td>
<td>Leave Total</td>
</tr>
#foreach (var item in Model.LeaveDetailsList)
{
<tr>
<td>#Html.TextBoxFor(m => item.LeaveType, new { width = "100" })</td>
<td>#Html.TextBoxFor(m => item.LeaveTaken, new { width = "100" })</td>
<td>#Html.TextBoxFor(m => item.LeaveBalance, new { width = "100" })</td>
<td>#Html.TextBoxFor(m => item.LeaveTotal, new { width = "100" })</td>
</tr>
}
</table>
I want to iterate through all the html table rows and insert the values in ADO.NET DataTable.
Simple speaking, converting HTML Table to ADO.NET DataTable.
How to extract values from HTML Table and insert into ADO.NET DataTable?
The view is based on the following model
public class LeaveBalanceViewModel
{
public LeaveBalanceViewModel()
{
this.EmployeeDetail = new EmployeeDetails();
this.LeaveBalanceDetail = new LeaveBalanceDetails();
this.LeaveDetailsList = new List<LeaveBalanceDetails>();
}
public EmployeeDetails EmployeeDetail { get; set; }
public LeaveBalanceDetails LeaveBalanceDetail { get; set; }
public List<LeaveBalanceDetails> LeaveDetailsList { get; set; }
}

In order to bind to a model on post back, the name attributes of the form controls must match the model properties. Your use of a foreach loop does not generate the correct name attributes. If you inspect the html you will see multiple instances of
<input type="text" name="item.LeaveType" .../>
but in order to bind to your model the controls would need to be
<input type="text" name="LeaveDetailsList[0].LeaveType" .../>
<input type="text" name="LeaveDetailsList[1].LeaveType" .../>
etc. The easiest way to think about this is to consider how you would access the value of a LeaveType property in C# code
var model = new LeaveBalanceViewModel();
// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value
var leaveType = model.LeaveDetailsList[0].LeaveType;
Since your POST method will have a parameter name (say model), just drop the prefix (model) and that's how the name attribute of the control must be. In order to do that you must use either a for loop (the collection must implement IList<T>)
for(int i = 0; i < Model.LeaveDetailsList.Count; i++)
{
#Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)
....
}
or use a custom EditorTemplate (the collection need only implement IEnumerable<T>)
In /Views/Shared/EditorTemplates/LeaveBalanceDetails.cshtml
#model yourAssembly.LeaveBalanceDetails
<tr>
<td>#Html.TextBoxFor(m => m.LeaveType)</td>
....
</tr>
and then in the main view (not in a loop)
<table>
.... // add headings (preferably in a thead element
<tbody>
#Html.EditorFor(m => m.LeaveDetailsList)
</tbody>
</table>
and finally, in the controller
public ActionResult Edit(LeaveBalanceViewModel model)
{
// iterate over model.LeaveDetailsList and save the items
}

With respect to your requirement, try this
jQuery(document).on("change", ".DDLChoices", function (e) {
var comma_ChoiceIds = '';
var comma_ChoicesText = '';
$('input[class="DDLChoices"]').each(function (e) {
if (this.checked) {
comma_ChoiceIds = comma_ChoiceIds + $(this).val() + ',';
comma_ChoicesText = comma_ChoicesText + $(this).parent('label').parent() + ',';
}
});
$('#ChoiceIds').val(comma_ChoiceIds);
$('#ChoiceText').val(comma_ChoicesText);
});
#using (Html.BeginForm("Actionname", "Controllername", FormMethod.Post, new { id = "frmChoices" }))
{
#Html.HiddenFor(m => m.ChoiceText, new { #id = "ChoiceText" })
#Html.HiddenFor(m => m.ChoiceIds, new { #id = "ChoiceIds" })
<div class="form-group">
<div>
<table>
<tr>
<th>Name</th>
<th>Selected</th>
</tr>
#foreach (var item in #Model.Choices)
{
<tr>
<td> <label>#item.ChoicesText</label> </td>
<td> <input class="DDLChoices" value="#item.ChoiceIds" type="checkbox" /></td>
</tr>
}
</table>
</div>
<input type="button" value="Submit" onclick="return ChoicesPoster.passChoices()"
</div>
}

Related

data is not deleted after click on Delete Button in ASP.NET MVC

I created view where i listed my all records and i wanted to delete particular record after click on delete Button . But my record is deleted.
Following is the code-
Controller code-
public ActionResult Faqdelete()
{
var res = dbobj.tbl_FAQ.ToList();
return View(res);
}
[HttpPost]
public ActionResult Faqdelete(int id)
{
var res = dbobj.tbl_FAQ.Where(x => x.FAQ_ID == id).FirstOrDefault();
dbobj.tbl_FAQ.Remove(res);
dbobj.SaveChanges();
var Faqdata = dbobj.tbl_FAQ.ToList();
return View("Faqdelete",Faqdata);
}
- View page Code-
#model IEnumerable<ClassOne_SampleProject.Models.tbl_FAQ>
#{
ViewBag.Title = "Faqdelete";
}
<h2>Faqdelete</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FAQ_Question_Answer)
</th>
<th>Action</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.Raw(item.FAQ_Question_Answer)
</td>
<td>
Delete
</td>
</tr>
}
</table>
Please tell me where exactly the problem.
Modify your anchor tag as
<td>
<a href="Url.Action("Faqdelete", "somecontroller", new { id = item.FAQ_ID })"
class="btn btn-danger">Delete</a>
</td>

Displaying values of a view to another view (MVC)

I have a table in a View with some Car details on it. After I click 'Rent' button, to redirect me to another View and display the data of the selected row there. Using the button that I already have on the code, I am only able to display details of that row in the URL but they are not being displayed on the page.
Here is my ListOfCars View:
#model IEnumerable<CarDataAccess.Car>
#{
ViewBag.Title = "List";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ListCars</title>
<script src="~/Scripts/OnClick.js">
</script>
</head>
<body>
#*<p>
#Html.ActionLink("Create New", "Post")
</p>*#
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.model)
</th>
<th>
#Html.DisplayNameFor(model => model.make)
</th>
<th>
#Html.DisplayNameFor(model => model.location)
</th>
<th>
#Html.DisplayNameFor(model => model.avaStart)
</th>
<th>
#Html.DisplayNameFor(model => model.avaEnd)
</th>
<th>
#Html.DisplayNameFor(model => model.price)
</th>
<th>
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.model)
</td>
<td>
#Html.DisplayFor(modelItem => item.make)
</td>
<td>
#Html.DisplayFor(modelItem => item.location)
</td>
<td>
#Html.DisplayFor(modelItem => item.avaStart)
</td>
<td>
#Html.DisplayFor(modelItem => item.avaEnd)
</td>
<td>
#Html.DisplayFor(modelItem => item.price)
</td>
<td>
<td>#Html.ActionLink("Rent", "Payment", new { id = item.Id })</td>
</td>
</tr>
}
</table>
<div>
Login
</div>
And here is the other View where I want the row to be displayed:
#model IEnumerable<CarDataAccess.Car>
#{
/**/
ViewBag.Title = "Payment";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Payment</title>
</head>
<body>
<table class="paymenttable">
</table>
</body>
</html>
Controller's code:
namespace RentCarApplication.Controllers
{
public class HomeController : Controller
{
BookCarDBEntities db = new BookCarDBEntities();
public ActionResult Index(int? id)
{
ViewBag.Title = "Home Page";
return View();
}
public ActionResult ListCars()
{
string username = User.Identity.Name;
var cars = db.Cars.ToList();
return View(cars);
}
public ActionResult Payment(int id)
{
using (BookCarDBEntities entities = new BookCarDBEntities())
{
var entity = entities.Cars.FirstOrDefault(c => c.Id == id);
if (entities != null)
{
return View(entity);
}
else
{
return View("Not Found");
}
}
}
}
}
In the controller action Payment(int id) you are passing a single entity of type CarDataAccess.Car to the view (the line in your code entities.Cars.FirstOrDefault(c => c.Id == id) gets a single entity from the database).
The Payment view expects a collection of type IEnumerable<CarDataAccess.Car>. You can see this on the first line of the view #model IEnumerable<CarDataAccess.Car>. Change this to just #model CarDataAccess.Car and see if that solves the problem.
Update the view to this:
#model CarDataAccess.Car
#{
/**/
ViewBag.Title = "Payment";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Payment</title>
</head>
<body>
#* access model properties here *#
<span>#Model.Id</span>
</body>
</html>
There is an error in your controller too. Check that the Car entity is not null, not the database context (entities):
public ActionResult Payment(int id)
{
using (BookCarDBEntities entities = new BookCarDBEntities())
{
var entity = entities.Cars.FirstOrDefault(c => c.Id == id);
if (entity != null) // check if entity is null, not entities
{
return View(entity);
}
else
{
return View("Not Found");
}
}
}
There is another issue in your controller too - you have declared multiple instances of your database context (BookCarDBEntities). It is best to wrap your database context in using statements like you did in the Payment method. Do the same for the ListCars method and remove BookCarDBEntities db = new BookCarDBEntities(); from the top of the controller. If you wrap the context in using statements then that ensures the context is properly disposed of and cleaned up by the garbage collector.
The ListCars method should look like:
public ActionResult ListCars()
{
string username = User.Identity.Name;
var cars = new List<CarDataAccess.Car>();
using (var db = new BookCarDBEntities())
{
cars = db.Cars.ToList();
}
return View(cars);
}

I'm having trouble getting data from razor page

I have a controller where i call a page with a data model.
//** Get all members
IEnumerable<mdMedlemmer> medlemsModels = _medlemmer.GetAll(_options.LoggedInGuid.ToString());
//** Get listing result
var ListingResult = medlemsModels.Select(result => new MedlemsIndexListingModel
{
Id = result.ID,
Navn = result.Navn,
LicensNummer = result.LicensNummer,
Niveau = result.Niveau
}).OrderBy(o => o.Navn);
//** Set model
var model = new MedlemsIndexModel
{
Medlems = ListingResult
};
//** Show view with model
return View(model);
In my cshtml page i have this code:
#model MinDartklub.Models.Medlemmer.MedlemsIndexModel
<table class="table table-condensed" id="medlemsIndexTable">
<thead>
<tr>
<th>Navn</th>
<th>Vælg</th>
</tr>
</thead>
<tbody>
#using (Html.BeginForm("Pujle", "Turnering", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
int counter = 1;
#foreach (var medlems in Model.Medlems)
{
<tr class="medlemsRow">
<td class=""><input type="text" navn="txtSpiller_#counter" id="txtSpiller_#counter" value="#medlems.Navn" disabled/></td>
<td class=""><input type="checkbox" name="cbSpiller_#counter" /></td>
</tr>
counter++;
}
<tr class="medlemsRow">
<td class="" colspan="2"><input class="btn btn-lg btn-info" type="submit" value="Start pulje" /></td>
</tr>
}
</tbody>
</table>
Back in the controller i try to read the data:
[HttpPost]
public ActionResult Pujle(string txtSpiller_1, string cbSpiller_1)
{
return RedirectToAction("Index", "Turnering");
}
The cshtml pagge shows all the members and a checkbox
In the controller Start() method I want to read all the members who have been checked.
Any idea on how I do that?
I found out I could use Request.Form.ToList()
This gives me a list of the member where I clicked in the checkbox.

Passing an ID from Razor view to Controller

In a view of type List I'm trying to pass the id (which is an eMail in this case) of the item from the view to an actionResult in the controller: I used the following code():
#Html.ActionLink("Contact", "CreateMessage", "Member", new { id = item.eMail })
I get a null reference because the id passed is null.
Here's the view which works just fine and the id in it isn't null:
#model IEnumerable<Mutuelle.Domain.Entities.Adherent>
#{
ViewBag.Title = "lister";
}
<h2>Liste des adhérents</h2>
<table class="table">
<tr>
<body style="overflow:scroll;">
<th>
#Html.DisplayNameFor(model => model.dateOfBirth)
</th>
<th>
#Html.DisplayNameFor(model => model.address)
</th>
<th>
#Html.DisplayNameFor(model => model.phoneNumber)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.dateOfBirth)
</td>
<td>
#Html.DisplayFor(modelItem => item.address)
</td>
<td>
#Html.DisplayFor(modelItem => item.phoneNumber)
</td>
<td>
#Html.ActionLink("Contacter", "CreateMessage", "MembreComite", new { id = item.eMail })
</td>
</tr>
}
</table>
Here's the controller:
public ActionResult CreateMessage(string id)
{
Message message = new Message();
return View(message);
}
[HttpPost]
public ActionResult CreateMessage(Message message, string id)
{
if (ModelState.IsValid)
{
AMDMService service = new AMDMService();
Member member = service.GetMemberByeMail(id);
message.member = member;
message.adherentId = id;
member.listMessages.Add(message);
return RedirectToAction("listeMessages");
}
return View(message);
}
When you use routeValues like new { id = item.eMail } in your ActionLink, then you always should provide value for html attributes, which mostly used for styling your link, so if you don't want to style your link then you pass null otherwise you pass something like that new { #class = "btn btn-rounded btn-blue" }). So your code for ActionLink should look like this if you don't want to style your link:
#Html.ActionLink("Contact", "CreateMessage", "Member", new { id = item.eMail }, null);
Also you can replace #Html.ActionLink helper with #Url.Action helper like below:
Contact

use a model as IEnumerable and static in same view

Am trying to put together a view where it will generate a detail list from a table through code first and a strongly typed view but also allow the user to input data into through the model into the table.
So the first part is:
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Gender)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.GenderID }) |
#Html.ActionLink("Details", "Details", new { id = item.GenderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.GenderID })
</td>
</tr>
}
</table>
The next part to enter a new gender would be:
<div>#Html.LabelFor(model => model.Gender)</div>
<div>#Html.EditorFor(model => model.Gender, new { htmlAttributes = new { #class = "div-form-control", id = "LkUpGender" } })</div>
<div>#Html.ValidationMessageFor(model => model.Gender, "", new { #class = "text-danger" })</div>
The problem is the first part requires:
IEnumerable<OrwellFrontEnd.LkUpModels.vwLkUpGender>
But the second part seems to not work with that and require:
OrwellFrontEnd.LkUpModels.vwLkUpGender
How can I make this work?