ASP.NET MVC 4和MVCContrib网格:渲染复选框在复杂类型的每个TR和邮政值 [英] ASP.NET MVC 4 and MVCContrib Grid: Render Checkbox for each TR and Post values in complex type
问题描述
MVCContrib网格:
@model ViewModel
@using (Html.BeginForm("SendData", "Home", FormMethod.Post))
{
@Html.Grid(Model.List).Columns(c =>
{
c.For(x => x.Id);
c.For(x => x.Name);
c.For(x =>Html.Partial("Partial/CheckBoxTemplate", new CheckBoxViewModel { Id = x.Id })).Named("Options");
})
@Html.SubmitButton()
}
控制器POST操作:
public ActionResult SendData(List<CheckBoxViewModel> list)
{
return View();
}
的ViewModels:
public class CheckBoxViewModel
{
public int Id { get; set; }
public bool CheckBox { get; set; }
}
public class ViewModel
{
public IPagination<Data> List { get; set; }
}
public class Data
{
public int Id { get; set; }
public string Name { get; set; }
}
局部视图:
@model MvcApplication1.Models.CheckBoxViewModel
@Html.HiddenFor(x => x.Id)
@Html.CheckBoxFor(x => x.CheckBox)
所有checkboxed默认情况下不进行检查。
All checkboxed are by default not checked.
我怎样才能找回我的的SendData code>操作的所有选中的复选框值?
How can I retrieve all checked checkbox values in my SendData
action?
推荐答案
我会建议你使用,这将反映您观的要求(显示包含的每一行复选框一格,并显示该ID真实视图模型与项目名称和回传动作检索列表中的这些值):
I would recommend you to use a real view model that will reflect the requirements of your view (displaying a grid containing for each row a checkbox, and displaying the id and name of the item and retrieving those values in a list in your postback action):
public class CheckBoxViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool CheckBox { get; set; }
}
public class ViewModel
{
public IPagination<CheckBoxViewModel> List { get; set; }
}
,然后让你的视图强类型此视图模型:
and then have your view strongly typed to this view model:
@model ViewModel
@using (Html.BeginForm("SendData", "Home", FormMethod.Post))
{
@Html.Grid(Model.List).Columns(c =>
{
c.For(x => x.Id);
c.For(x => x.Name);
c.For(x => Html.Partial("Partial/CheckBoxTemplate", x)).Named("Options");
})
<button type="submit">OK</button>
}
最后你的局部看起来是这样的:
finally your partial could look like this:
@model CheckBoxViewModel
@{
var index = Guid.NewGuid().ToString();
}
@Html.Hidden("list.Index", index)
@Html.Hidden("list[" + index + "].Id", Model.Id)
@Html.Hidden("list[" + index + "].Name", Model.Name)
@Html.CheckBox("list[" + index + "].CheckBox", Model.CheckBox)
现在调用的SendData code>操作时,它会通过您的视图模型的列表。
Now when the SendData
action is invoked it will be passed the list of your view model.
另外,您可以使用在 Html.BeginCollectionItem 帮手$ P $ =http://blog.stevensanderson.com/2010/01/ 28 /编辑-A-可变长度列表的ASPNET-MVC-2型/相对=nofollow> 下面的文章
这将允许您使用助手的强类型版本:
Alternatively you could use the Html.BeginCollectionItem
helper presented in the following article
which would allow you to use the strongly typed versions of the helpers:
@model CheckBoxViewModel
@using(Html.BeginCollectionItem("list"))
{
@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.Name)
@Html.CheckBoxFor(x => x.CheckBox)
}
建议进一步阅读: 模型绑定到一个List
。
这篇关于ASP.NET MVC 4和MVCContrib网格:渲染复选框在复杂类型的每个TR和邮政值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!