具有对象列表作为属性的 MVC 模型 [英] MVC Model with a list of objects as property
问题描述
我正在开发一个 MVC 应用程序,其中模型类 Item
有一个名为 AvailableColours
的 List
作为属性.>
AvailableColours
是用户定义的 Colour
类子集.我想在复选框列表中显示所有 Colour
实例,当提交时,AvailableColours
是一个 List
包含选中的 <代码>颜色代码>类.
在 MVC 中执行此操作的最佳方法是什么?
我的代码到目前为止,虽然我觉得这不是最 MVC-ish 的方式!
模型
公共类项目{公共 int ID { 获取;放;}公共列表<颜色>可用颜色 { 获取;放;}}
查看
@model MyNamespace.Models.Item@using MyNamespace.Models;@{ViewBag.Title = "创建";var allColours = new List();//从数据库中检索,但为简单起见省略}<h2>创建新项目</h2>@using (Html.BeginForm("Create", "Item", FormMethod.Post)){<div>@Html.LabelFor(model => model.AvailableColours)@foreach(allColours 中的 var 颜色){<input type="checkbox" name="colours" value="@colour.Description"/>}
<input type="submit" value="Submit"/>}
控制器
[HttpPost]公共 ActionResult 创建(项目项目,字符串 [] 颜色){尝试{foreach (var color in colour){item.AvailableColours.Add(GetColour(colour));//从数据库中检索return RedirectToAction("索引");}}抓住{返回视图();}}
模型
公共类项目{公共列表<颜色>可用颜色 { 获取;设置;}}公开课颜色{公共 int ID { 获取;放;}公共字符串 描述 { 获取;放;}公共布尔检查{得到;放;}}
注意 Checked
属性
查看循环
@using (Html.BeginForm("Create", "Item", FormMethod.Post)){<div>@Html.LabelFor(model => model.AvailableColours)@for(var i = 0; i < Model.AvailableColours.Count; i++){@Html.HiddenFor(m => Model.AvailableColours[i].ID)@Html.HiddenFor(m => Model.AvailableColours[i].Description)@Html.CheckBoxFor(m => Model.AvailableColours[i].Checked)@Model.AvailableColours[i].Description<br/>}
<input type="submit" value="Submit"/>}
注意 foreach 的 for 循环以启用模型绑定和隐藏字段以允许将值回发到控制器
控制器帖子
[HttpPost]公共 ActionResult 创建(项目模型){//所有选中的都在AvailableColours中可用返回视图(模型);}
I am working on an MVC application where the Model class Item
has a List<Colour>
named AvailableColours
as a property.
AvailableColours
is a user defined subset of Colour
classes. I would like to display all Colour
instances in a check box list, and when submitted, AvailableColours
is a List<Colour>
containing the checked Colour
classes.
What is the best way to do this in MVC?
Edit: My code so far, although I feel this is not the most MVC-ish way to do it!
Model
public class Item
{
public int ID { get; set; }
public List<Colour> AvailableColours { get; set; }
}
View
@model MyNamespace.Models.Item
@using MyNamespace.Models;
@{
ViewBag.Title = "Create";
var allColours = new List<Colour>(); //retrieved from database, but omitted for simplicity
}
<h2>Create New Item</h2>
@using (Html.BeginForm("Create", "Item", FormMethod.Post))
{
<div>
@Html.LabelFor(model => model.AvailableColours)
@foreach (var colour in allColours)
{
<input type="checkbox" name="colours" value="@colour.Description" />
}
</div>
<input type="submit" value="Submit" />
}
Controller
[HttpPost]
public ActionResult Create(Item item, string[] colours)
{
try
{
foreach (var colour in colours)
{
item.AvailableColours.Add(GetColour(colour));//retrieves from database
return RedirectToAction("Index");
}
}
catch
{
return View();
}
}
Models
public class Item
{
public List<Colour> AvailableColours { get;set; }
}
public class Colour
{
public int ID { get; set; }
public string Description { get; set; }
public bool Checked { get; set; }
}
Note the Checked
property
View for loop
@using (Html.BeginForm("Create", "Item", FormMethod.Post))
{
<div>
@Html.LabelFor(model => model.AvailableColours)
@for(var i = 0; i < Model.AvailableColours.Count; i++)
{
@Html.HiddenFor(m => Model.AvailableColours[i].ID)
@Html.HiddenFor(m => Model.AvailableColours[i].Description)
@Html.CheckBoxFor(m => Model.AvailableColours[i].Checked)
@Model.AvailableColours[i].Description<br/>
}
</div>
<input type="submit" value="Submit" />
}
Note the for loop insted of foreach to enable model binding and the hidden fields to allow the values to be posted back to the controller
Controller post
[HttpPost]
public ActionResult Create(Item model)
{
//All the selected are available in AvailableColours
return View(model);
}
这篇关于具有对象列表作为属性的 MVC 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!