Asp.net mvc 3 从razor视图引擎中动态创建的复选框列表中获取复选框的值

Asp.net mvc 3 从razor视图引擎中动态创建的复选框列表中获取复选框的值,asp.net-mvc-3,Asp.net Mvc 3,如何从razor视图引擎中动态创建的复选框列表中查找复选框的值(即,是否选中)?代码运行如下 @foreach (var item in Model)) { <tr> <td class="Viewtd"> @Html.ActionLink(item.Title, "Edit", new { id = item.id}) </td> <td>

如何从razor视图引擎中动态创建的复选框列表中查找复选框的值(即,是否选中)?代码运行如下

@foreach (var item in Model))
{
       <tr>
          <td class="Viewtd">
              @Html.ActionLink(item.Title, "Edit", new { id = item.id})
          </td>
          <td>
     @Html.CheckBox("ChkBox"+item.ThresholdID , false, new { id = item.id})                         
          </td>
       </tr>
}
@foreach(模型中的变量项))
{
@ActionLink(item.Title,“编辑”,新的{id=item.id})
@复选框(“ChkBox”+item.ThresholdID,false,new{id=item.id})
}

如何在控制器中获取这些复选框值?

使用正确的方法:使用视图模型和编辑器模板

一如既往,从定义视图模型开始:

public class MyViewModel
{
    public string Title { get; set; }
    public string Id { get; set; }
    public bool IsThreshold { get; set; }
}
然后是一个用于填充此视图模型的控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new[] 
        {
            new MyViewModel 
            {
                Id = "1",
                Title = "title 1",
                IsThreshold = false,
            },
            new MyViewModel 
            {
                Id = "2",
                Title = "title 2",
                IsThreshold = true,
            },
            new MyViewModel 
            {
                Id = "3",
                Title = "title 3",
                IsThreshold = false,
            },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Edit(MyViewModel model)
    {
        // This action will be responsible for editing a single row
        // it will be passed the id of the model and the value of the checkbox
        // So here you can process them and return some view
        return Content("thanks for updating", "text/plain");
    }
}
然后是索引视图(
~/Views/Home/Index.cshtml
):


使用正确的方法:使用视图模型和编辑器模板

一如既往,从定义视图模型开始:

public class MyViewModel
{
    public string Title { get; set; }
    public string Id { get; set; }
    public bool IsThreshold { get; set; }
}
然后是一个用于填充此视图模型的控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new[] 
        {
            new MyViewModel 
            {
                Id = "1",
                Title = "title 1",
                IsThreshold = false,
            },
            new MyViewModel 
            {
                Id = "2",
                Title = "title 2",
                IsThreshold = true,
            },
            new MyViewModel 
            {
                Id = "3",
                Title = "title 3",
                IsThreshold = false,
            },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Edit(MyViewModel model)
    {
        // This action will be responsible for editing a single row
        // it will be passed the id of the model and the value of the checkbox
        // So here you can process them and return some view
        return Content("thanks for updating", "text/plain");
    }
}
然后是索引视图(
~/Views/Home/Index.cshtml
):