c# - How to model bind from view to controller via nested view -
i cant seem pass employee id value check box in employees.cshtml view delete post action method. delete action method returns delete.cshtml view renders employees.cshtml in editortemplates folder under shared folder. when click submit cannot seem pass ienumerable of checked @model.ids.
what want delete every entry has been checked checked value derived in employees.cshtml.
obviously issue model binding (i think) done button submit delete.cshtml page, how can change this?
the delete action method.
[httpget] public actionresult delete() { return view(db2.employees.tolist()); } the delete post action method.
[httppost] public actionresult delete(ienumerable<int> employeeidtodelete) { if (employeeidtodelete != null) { var employeestodelete = db2.employees.where(x => employeeidtodelete.contains(x.id)).tolist(); foreach (var item in employeestodelete) { db2.employees.remove(item); } db2.savechanges(); redirecttoaction("delete"); } return view(db2.employees.tolist()); } my delete view
@model ienumerable<mvc_example2___ado.models.employees> @{ viewbag.title = "delete"; } <html> <body> @using (html.beginform()) { <table align="center"> <thead> <tr> <td>check</td> <td>photo</td> <td>name</td> <td>gender</td> </tr> </thead> <tbody> @html.editorformodel() </tbody> </table> <input type="submit" name="submit" value="delete entries" /> } </body> </html> my employees view
@model mvc_example2___ado.models.employees <tr> <td><input type="checkbox" name="employeeidstodelete" id="employeeidstodelete" value="@model.id" /></td> <td>@html.image(@model.photo, @model.alternatetext, 125, 130)</td> <td>@model.fullname</td> <td>@model.gender</td> </tr>
you have named checkboxes name="employeeidstodelete" parameter of post method ienumerable<int> employeeidtodelete (not plural). names must match.
note should remove id="employeeidstodelete" input since generating duplicate id attributes invalid html.
Comments
Post a Comment