How to transfer a list from a view to a controller

I have a problem transferring the list in the object from the view to the controller, the list is always zero. How can I do it?

My model:

public class ImageModel { public int id { get; set; } public string url { get; set; } public bool delete { get; set; } } public class SchoolImages { public Nullable<int> schoolid { get; set; } public IList<ImageModel> images; } 

View:

 @model SchoolAppManager.Models.SchoolImages @using (Html.BeginForm("DeleteImages", "Images")) { @Html.HiddenFor(x => x.schoolid) <table> @for (int i = 0; i < Model.images.Count(); i += 4) { <tr> <td> <img src="@Url.Content(Model.images[i].url)" width="50%" /> <br /> @Html.CheckBoxFor(x => x.images[i].delete) @Html.HiddenFor(x => x.images[i].id) </td> <td> @if (i + 1 < Model.images.Count()) { <img src="@Url.Content(Model.images[i + 1].url)" width="50%" /> <br /> @Html.CheckBoxFor(x => x.images[i + 1].delete) @Html.HiddenFor(x => x.images[i + 1].id) } </td> <td> @if (i + 2 < Model.images.Count()) { <img src="@Url.Content(Model.images[i + 2].url)" width="50%" /> <br /> @Html.CheckBoxFor(x => x.images[i + 2].delete) @Html.HiddenFor(x => x.images[i + 2].id) } </td> <td> @if (i + 3 < Model.images.Count()) { <img src="@Url.Content(Model.images[i + 3].url)" width="50%" /> <br /> @Html.CheckBoxFor(x => x.images[i + 3].delete) @Html.HiddenFor(x => x.images[i + 3].id) } </td> </tr> } </table> <input type="submit" value="delete" /> } 

When I SchoolImages delete, SchoolImages is passed to the controller, SchoolImages.schoolId has a value in the controller, however SchoolImages.images is null. How to pass SchoolImages.images to a controller?

+7
list asp.net-mvc asp.net-mvc-4
source share
4 answers

The middleware does not work with fields by default, so the make images property:

 public IList<ImageModel> images { get; set; } 
+6
source share

I think you will have a problem with binding complex types to your model. If you do not. Here is an alternative way.

You have a set of checkboxes, possibly with the same name and different values. You can send them to a method that takes a FormCollection, i.e.

 public ActionResult Test(FormCollection collection) { string results = collection["Blanks"]; } 

This will give you a comma-separated list of values ​​(or null if the checkboxes are unchecked).

Alternatively, if you have possible values ​​in the form of an array on the server, you can specify the flag names according to their array values, which would mean that you could do something like this:

 @using (Html.BeginForm("Test","Home")) { @Html.CheckBox("Blanks[0]", false); @Html.CheckBox("Blanks[1]", false); @Html.CheckBox("Blanks[2]", false); <input type="submit" value="Submit" /> } 

gives you an array of logic elements in a test method:

public test ActionResult (bool [] Forms) {}

+1
source share

Try changing the "SchoolImages" model; use an array instead of IList <>.

0
source share

This value is null because the middleware does not know by default how to initialize it. Change the field images as properties and add a constructor similar to this:

 public class SchoolImages { public SchoolImages() { images = new List<ImageModel>(); } public Nullable<int> schoolid { get; set; } public IList<ImageModel> images { get; set; } } 

It should work.

0
source share

All Articles