Binding IPagedList with list box checkbox

So, I have a class that has a List . I pass it in the form as below:

 [HttpGet] [Authorize(Roles="user")] [CustomChecker] public ActionResult Index(int? page, int id=0) { EmployeeContext emp = new EmployeeContext(); student st = emp.students.Single(x=>x.id ==id); @ViewBag.id = st.id; return View(st.subjSel.ToPagedList(page ?? 1, 4)); } 

And then the view will receive it as follows:

 @using PagedList; @using PagedList.Mvc; @model PagedList<MvcApplication6.Models.subject> <div style="font-family:Arial"> <fieldset> <legend><h3>Open Classes</h3></legend> @using (Html.BeginForm("Test", "Enrollment")) { <input type="hidden" name="id" value="@ViewBag.id" /> <table border="1"> <tr> <th>@Html.LabelFor(model => model[0].subj)</th> <th>@Html.LabelFor(model => model[0].days)</th> <th>@Html.LabelFor(model => model[0].cstart)</th> <th>@Html.LabelFor(model => model[0].cend)</th> <th>@Html.LabelFor(model => model[0].professor)</th> <th>@Html.LabelFor(model => model[0].units)</th> <th>@Html.CheckBox("test") Select all</th> </tr> @for (int i = 0; i < Model.Count; i++) { <tr> @Html.HiddenFor(model => model[i].id) <td> @Html.DisplayFor(m => m[i].subj) @Html.HiddenFor(m => m[i].subj) </td> <td> @Html.DisplayFor(m => m[i].days) @Html.HiddenFor(m => m[i].days) </td> <td> @Html.DisplayFor(m => m[i].cstart) @Html.HiddenFor(m => m[i].cstart) </td> <td> @Html.DisplayFor(m => m[i].cend) @Html.HiddenFor(m => m[i].cend) </td> <td> @Html.DisplayFor(m => m[i].professor) @Html.HiddenFor(m => m[i].professor) </td> <td> @Html.DisplayFor(m => m[i].units) @Html.HiddenFor(m => m[i].units) </td> <td> @Html.CheckBoxFor(m => m[i].isSelected) </td> </tr> } </table> <br /> <br /> <table> <tr><td align="center" width="500px"></td></tr> <tr> <td align="center" width="500px"> <input type="submit" value="submit" /> | <input type="button" value="clear" /> </td> </tr> </table> <br /> <br /> } </fieldset> </div> @Html.PagedListPager(Model, page => Url.Action("Index", "Enrollment", new { page, id = Request.QueryString["id"] })) 

My problem is that it will display as [0].subj , and this will not allow me to communicate, because it should be something like name[0].subj .

I experimented and tried new methods, do I have ways to link them correctly? I want to use Html Helpers and as much as possible, I do not want to re-implement custom for this part only.

This is the function in which they should be connected. This class has a List students (the one I converted to IPagedList)

 [HttpPost] [Authorize(Roles="user")] public ActionResult Test(student st) 

And this is what my View looks like. I use CheckBoxFor to select. enter image description here

Additional question: Why does my navigation look so ugly?

+6
source share
2 answers

The model is a view of @model PagedList<subject> , which means that the parameter must be

 [HttpPost] public ActionResult Test(IEnumerable<subject> model) 

If you also need the student ID property, add an additional parameter

 [HttpPost] public ActionResult Test(IEnumerable<subject> model, int ID) 

Since your GET method has an int ID parameter and it is assumed that you are using standard routing, then the identifier will be added to the action attribute, i.e. will display action="/Enrollment/Test/2" if the value is ID 2 , if not, you can add this as a route parameter

 @using (Html.BeginForm("Test", "Enrollment", new { ID = ViewBag.id })) 

Alternatively you can use the view model

 public class StudentVM { public int ID { get; set; } public PagedList<student> Students { get; set; } } 

and in the get method

 public ActionResult Index(int? page, int id=0) { EmployeeContext emp = new EmployeeContext(); student st = emp.students.Single(x=>x.id ==id); StudentVM model = new StudentVM() { ID = id, Students = st.subjSel.ToPagedList(page ?? 1, 4) }; return View(model); } 

and create a base for viewing the model and return it back to the public ActionResult Test(StudentVM model)

+1
source

To get the subject list back, you must apply the appropriate prefix to it. Since the Student property name containing the List<subject> is subjSel , and you want to get the values ​​in your action in the Student object, so you must set a prefix like this before the for expression:

 @{ViewData.TemplateInfo.HtmlFieldPrefix = "subjSel";} 

Thus, model files will be displayed with names like subjSel[0].fieldname .

Then, in your post action, you will receive items in the subjSel property.

Additional resource:

For your additional question:

Check if your rendered code matches the appropriate style, and your style tags related to the search call are displayed on the page.

+1
source

All Articles