I have an MVC 5 application that retrieves all records from an Entity and generates a table from those records. The view is populated like so:
/get all data from the Entity using a generic method
public async Task UnverifiedDataTableAsync(
AlertModel alert,string viewName = null) where TEntity : class
{
// get data
var data = (from a in await Manager.AllAsync() //Manager.AllAsyncgathers all records from the EF classselect a).ToList();
// create sample log model with alert
var response = new TableModel
{
Alert = alert,
Data = data
};
// return partial data view
return PartialView(viewName ?? $"_Unverified{typeof(TEntity).Name}Table", response);
}
This is the method that's called from the controller that displays the populated view with data
public async Task UnverifiedDrinkingWaterLog(AlertModel alert)
{
//return partial Drinking water log view
return await UnverifiedDataTableAsync(alert);
}
The model for the view is the EF class itself so the properties come from the Database.
@model MyApplication.Application.TableModel.DrinkingWater>
@Html.AntiForgeryToken();
| @Html.LabelFor(m => m.Data.FirstOrDefault().Location)th> //{...} other properties left out for brevity @Html.LabelFor(m => m.Data.FirstOrDefault().Verified)th> | tr> thead> @for (int i = 0; i < Model.Data.Count(); i++){ @Html.HiddenFor(m => m.Data[i].Id) @Html.TextBoxFor(m => m.Data[i].Location)td> | //{...} other properties left out for brevity @Html.HiddenFor(m => m.Data[i].Verified) @Html.CheckBoxFor( | v => v.Data[i].Verified) td> tr> } tbody> table> form> And then the controller that sets the fields to update for the current record [HttpPost] [ValidateAntiForgeryToken] public async Task { //create alert model var response = new AlertModel(); //check model state if (ModelState.IsValid) { //iterate over each record that's in the model that's passed from the view foreach(var record in model) { //check the Verified check box status that's passed from the view //then update the record } else { InvalidState(response); } //return the table with that shows the record(s) were updated return await UnverifiedDrinkingWaterLog(response); } When I run this the model that's passed to the controller is always null. Am I missing some kind of binding between the model and the Entity data? How can I pass the entire entity data that's generated in the VerifiedDataTableAsync method, to the VerifyDrinkingWater action that reads input from the table (it will be only a checkbox) and then updates the record in the database? Similar to this link however the data is already in a list coming from the UnverifiedDataTableAsync method. https://www.c-sharpcorner.com/article/pass-dynamically-added-html-table-records-list-to-controller/ 1 ReplyKnow the answer? Post it — somebody with the same question will find it here. Sign in to answer this question It is the same account you read, post and publish with — and you will come straight back to this page. |
|---|
Jenith ThakkarPosted Aug 16, 2018, 12:09 AM
in your view you passing DrinkingWater as a Model like this
@model MyApplication.Application.TableModel.DrinkingWater