简体   繁体   English

如何通过清单 <model> 从控制器在MVC4中查看?

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

How to pass List<model> from view to controller?. 如何将List<model>从视图传递到控制器? See below example, @Html.ActionLink . 参见以下示例@Html.ActionLink I want what are the checkboxes user selected after clicking delete. 我想要单击删除后checkboxes用户。

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;
}

Unfortunately it won't work that way. 不幸的是,它不会那样工作。

You would need to keep the List in Session (or TempData) 您需要将列表保留在会话中(或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 在控制器的httppost操作中,您将会话转换回列表,然后遍历表单中的复选框,然后再保存回数据库。

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 此处提供更多信息: http : //www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Edit: 编辑:

I should also make note that the model binder will work with JSON notation as well 我还应该注意,模型绑定程序也可以使用JSON表示法

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

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. 这不是可以将页面状态发送回服务器的ASP.NET Web窗体。 For something like this, I'd use ajax to send an array of the id's back to the controller, and do my del 对于这样的事情,我将使用ajax将ID返回的数组发送回控制器,然后执行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: 您的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);
       });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM