Asp.net mvc Asp.net Mvc如何在不设置控制器中选定的
我有一个带有子模型列表的父模型。在视图中,我使用这样的模板渲染子对象Asp.net mvc Asp.net Mvc如何在不设置控制器中选定的,asp.net-mvc,Asp.net Mvc,我有一个带有子模型列表的父模型。在视图中,我使用这样的模板渲染子对象 @Html.EditorFor(model => model.ChildItems) @Html.DropDownListFor(model => model.Quantity, new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 })) 在子模型模板中,我有一个下拉列表 @Html.DropDownListFor(model => model.Quant
@Html.EditorFor(model => model.ChildItems)
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
在子模型模板中,我有一个下拉列表
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
但我不知道如何设置所选项目
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
我看到的大多数示例都是简单的控制器创建列表、设置选定项并传递到视图
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
除非我在控制器中做类似的事情
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
ViewBag["Child_1_SelectedItem"] = children[0].Quantity
ViewBag["Child_2_SelectedItem"] = children[1].Quantity
我根本不知道如何让视图理解这一点。您通常会这样做:
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
var quantities = new List<SelectListItem>(new[] {
new SelectListItem {
Selected = true,
Text="1",
Value="1"
},
new SelectListItem {
Text="2",
Value="2"
}
....
看起来很烦人,但您可以在控制器上构建以下内容:
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
var q = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<SelectListItem> list = new List<SelectListItem>();
foreach (var item in q)
{
if(children[item].Quantity)// dunno what is in this property...
{//if Quantity is bool you could directly set Selected then..
list.Add(new SelectListItem { Selected = true, Text = item.ToString() });
} else {
list.Add(new SelectListItem { Text = item.ToString() });
}
}
var q=new[]{1,2,3,4,5,6,7,8,9,10};
列表=新列表();
foreach(q中的var项目)
{
if(children[item].Quantity)//不知道此属性中有什么。。。
{//如果数量为布尔,则可以直接将其设置为选中。。
添加(新建SelectListItem{Selected=true,Text=item.ToString()});
}否则{
添加(新建SelectListItem{Text=item.ToString()});
}
}
然后将您的
列表向下传递到视图。或者您可以在视图本身的@{..}
代码块中执行此操作。在接受所选项的选择列表
构造函数上有另一个重载,因此您的第二个代码段就快到了
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
int[] array = { 1, 2, 3 };
var list = new SelectList(array, 1);
鉴于此,您能否将代码更改为:
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }, model.Quantity))
编辑:
看起来您无法在第一个参数之外访问lambda参数(这很有意义),但您应该能够做到这一点(假设Model与Model的类型相同,但有细微的区别):
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
或者,使用简单变量:
@Html.DropDownListFor(model => model.Quantity,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }))
@{
int selectedQuantity = Model.Quantity;
}
@Html.DropDownListFor(model => model.Id,
new SelectList(new[] { 1,2,3,4,5,6,7,8,9,10 }, selectedQuantity))
当我看到超负荷时,我想也是这样。然而,它奇怪地抱怨说,它不知道是什么型号。无法解析符号模型。谢谢Steve!!我刚刚创建了一个简单的示例项目,并得到了相同的实现。