James123
James123

Reputation: 11662

how to pass List<model> to controller from view in MVC4?

How to pass List<model> from view to controller?. See below example,@Html.ActionLink. I want what are the checkboxes user selected after clicking delete.

model

  public class Images
    {
        public string Imgs { get; set; }
        public bool chkSelected { get; set; }
    }

view

@model IEnumerable<Models.Images>

@using (Html.BeginForm())
{

 <span class="galleryImage">
   @foreach (var image in Model)
   {
     <a href="@Url.Content("~/ImageGallery/" + image.Imgs)"  data-gallery>
          <img src="@Url.Content("~/ImageGallery/thumbnails/" + image.Imgs)">
     </a>
      @Html.CheckBox("delete_CheckBox", false, new {Value = image.chkSelected})
   }
 </span>
 <span class="btn btn-primary fileinput-button">
    <i class="glyphicon glyphicon-trash"></i>
       @Html.ActionLink("Delete files...", "Delete", "Image") //Pass from here
 </span>
}

controller

[HttpGet]
[Route("Admin/Image/Delete")]
public ActionResult Delete(IEnumerable<Images> imageses)
{
  //Here Imageses are coming null
   return null;
}

Upvotes: 2

Views: 6171

Answers (3)

mnsr
mnsr

Reputation: 12437

Sorry, i wrote this asnwer and got side tracked, but i'll post anyway:

This isn't ASP.NET webforms where there is a page state that can be sent back to the server. For something like this, I'd use ajax to send an array of the id's back to the controller, and do my del

<span class="galleryImage">
   @foreach (var images in Model)
   {
     <a href="@Url.Content("~/ImageGallery/" + images.Imgs)"  data-gallery>
          <img src="@Url.Content("~/ImageGallery/thumbnails/" + images.Imgs)" id="myImages">
     </a>
      @Html.CheckBox("delete_CheckBox", false, new {Value = images.ID})
   }
 </span>
 <span class="btn btn-primary fileinput-button">
    <i class="glyphicon glyphicon-trash"></i>
       @Html.ActionLink("Delete files...", "Delete", "Image", new { @id = "delete" }) //Pass from here
 </span>

then set up your controller:

[HttpPost]
[Route("Admin/Image/Delete")]
public ActionResult Delete(int[] ids)
{
   foreach(int i in ids)
      // delete
   return Content("successfully deleted " + ids.Length + " items.");
}

your js:

$("#delete").click(function(e) {
   e.preventDefault();
   var arr = new Array();
   $("#myImages:checked").each(function () { arr.push($(this).val()); });
   $.post("admin/image/delete", { ids: arr },
       function(data) {
           alert(data);
       });

});

Upvotes: 1

Evonet
Evonet

Reputation: 3640

Unfortunately it won't work that way.

You would need to keep the List in Session (or TempData)

Session["imageses"] = list;

This way you might access it between requests.

EDIT

In your controller httppost action, you would convert the session back to a list, and then iterate through the checkboxes in your form, before saving back to the database

Upvotes: 1

Brent Mannering
Brent Mannering

Reputation: 2316

This can be done with Model Binding, you just need the correct syntax for the model.

<input type="hidden" value="1" name="images[0].Id"></input>
<input type="checkbox" checked="checked" name="images[0].Delete"></input>
<input type="hidden" value="2" name="images[1].Id"></input> 
<input type="checkbox" name="images[1].Delete"></input>

Essentially you just need to describe the array notation and the properties then the model binder will do the rest.

More info here: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Edit:

I should also make note that the model binder will work with JSON notation as well

[ 
    { Id : 1, Delete : true }, 
    { Id : 2, Delete : false } 
]

Upvotes: 1

Related Questions