dimanche 1 mars 2015

How to get the value of checkboxes in to the controller method in asp.net MVC

I am generating my Checkboxes from an IEnumerable model class like below.



<table class="table table-bordered">

<thead>
<tr>
<th>@Html.DisplayName("Id")</th>
<th>@Html.DisplayName("FirstName")</th>
<th>@Html.DisplayName("LastName")</th>
<th>@Html.DisplayName("Username")</th>
<th>@Html.DisplayName("Organization")</th>
<th>@Html.DisplayName("Active")</th>
<th>@Html.DisplayName("Block")</th>
<th>@Html.DisplayName("Action")</th>
</tr></thead>
@foreach (var item in Model.Employer) {
<tr>
<td>@Html.DisplayFor(modelItem=>item.Id)</td>
<td>@Html.DisplayFor(modelItem=>item.FirstName)</td>
<td>@Html.DisplayFor(modelItem=>item.LastName)</td>
<td>@Html.DisplayFor(modelItem=>item.Username)</td>
<td>@Html.DisplayFor(modelItem=>item.Organization)</td>
<td>@Html.CheckBoxFor(modelItem=>item.Active)</td>
<td>@Html.CheckBoxFor(modelItem=>item.Block)</td>



<td>
@using (Html.BeginForm("DeleteEmployer", "Admin"))
{
@Html.Hidden("employerId", item.Id)
<input type="submit" value="Delete"/>
}


</td>
<td>

@using (Html.BeginForm("UpdateEmployer", "Admin"))
{
@Html.Hidden("eId",item.Id)
@Html.Hidden("Active",item.Active)
@Html.Hidden("Block",item.Block)
<input type="submit" value="Update"/>
}

</td>
</tr>
}
</table>


I am attempting to get my ID and the values of the active and block checkboxes selections for a particular row on my Update button click. My id passed correctly. However, active and block selections are not changing. I Understand that my Active and Block being columns from my DB cannot be used for editing or strong-typing here. I thought of adding new columns to my model "SelectionActive" and "SelectionBlock" to my Employer model. However this results in so many error in my project for i used the model in many places and the two columns are not from a Db. Please how do i get the value of my Selections to my controller below.



public ActionResult UpdateEmployer(long Id, bool Active ,bool Block)
{

if (ModelState.IsValid)
{
repository.UpdateEmployer(Id,Active,Block);
TempData["Employermessage"] = string.Format("{0} was updated", Id);
}
return RedirectToAction("Index");
}


Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire