Beginner who needs help!! I have passed in a list of items to a view. Works great. I want to select
from this list with checkbox
and then gather data and pass it back to the Controller
for processing. I will loop
through the selections in the controller
and post appropriate data to the model
. At this point, I have tried enumeration in many different ways and simply cannot figure it out how to build the model
and pass the data back back. Please advise!!
The view: (hacked in in several formats, but this works to post the data on the view from the get in the controller)
View:
@model IEnumerable<eManager.Web2.Models.AddCompToEventClass>
@{
ViewBag.Title = "AddCompToClass";
}
<h2>AddCompToClass</h2>
@using (Html.BeginForm("AddCompToClass", "Compeditor", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CompLast)
</td>
<td>
@Html.DisplayFor(modelItem => item.Event_CompID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClassName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CompeditorID)
</td>
<td>
@Html.DisplayFor(modelItem => item.EventClassID)
</td>
<td>
@Html.DisplayFor(modelItem => item.EventName)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.IsSelected)
</td>
ViewModel feeding the above view: (Note: this is not data from an actual view, but a compound set of data gathered the controller get)
{
public class AddCompToEventClass
{
public int CompeditorID { get; set; }
public int Event_CompID { get; set; }
public string EventName { get; set; }
public int EventClassID { get; set; }
public string ClassName { get; set; }
public int EventID { get; set; }
public string CompLast { get; set; }
public int CompEventID { get; set; }
public bool IsSelected { get; set; }
}
}
Here is the specific area of the Controller's HTTPPOST:
[HttpPost]
public ActionResult AddCompToClass(AddCompToEventClass[] viewModel)
{
var eventclasscomp = new Event_Class_Compeditors();
{
if (ModelState.IsValid)
foreach (AddCompToEventClass items in viewModel) ;
After I execute the FormSubmit, I can see a single set of all values come into the controller, but they are all null or zero. I pass an object with 2 sets and show it but cannot understand how to rebuild the model and send it back to the controller.