TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Braden
1.7k
88
576
Passing table generated from an Entity to the controller
Aug 15 2018 2:48 PM
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<ActionResult> UnverifiedDataTableAsync<TEntity>(
AlertModel alert,string viewName = null) where TEntity : class
{
// get data
var data = (from a in await Manager.AllAsync<TEntity>() //Manager.AllAsyncgathers all records from the EF classselect a).ToList();
// create sample log model with alert
var response = new TableModel<TEntity>
{
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<ActionResult> UnverifiedDrinkingWaterLog(AlertModel alert)
{
//return partial Drinking water log view
return await UnverifiedDataTableAsync<DrinkingWater>(alert);
}
The model for the view is the EF class itself so the properties come from the Database.
@model MyApplication.Application.TableModel.DrinkingWater>
<form action="/DrinkingWater/VerifyDrinkingWater" method="post" id="verifyForm">
@Html.AntiForgeryToken();
<table id="UnverifiedDrinkingWaterTable" class="table table-hover">
<thead>
<tr>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Location)th>
//{...} other properties left out for brevity
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Verified)th>
tr>
thead>
<tbody>
@for (int i = 0; i < Model.Data.Count(); i++){ <tr>
@Html.HiddenFor(m => m.Data[i].Id)
<td>@Html.TextBoxFor(m => m.Data[i].Location)td>
//{...} other properties left out for brevity
@Html.HiddenFor(m => m.Data[i].Verified)<td>@Html.CheckBoxFor(
v => v.Data[i].Verified) td>
tr>
}
tbody>
table>
<hr />
<button id="VerifyButton" type="submit" class="btn btn-primary">Verifybutton>
form>
And then the controller that sets the fields to update for the current record
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> VerifyDrinkingWater(FPDrinkingWater drinkingWater)
{
//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/
Reply
Answers (
1
)
Problem to read the next file
Getting image from FormCollection in Controller (MVC)