[英]Dropdown list in Razor for array
I am trying to make a dropdownlist in Razor and the Razor code looks like this:我正在尝试在 Razor 和 Razor 代码中创建一个下拉列表,如下所示:
@for (int l = 0; l < Model.Entries.Count; l++)
{
@Model.Entries[l].StatusId
@Html.DropDownListFor(m => m.Entries[l].StatusId, Model.DropDownItems)
}
But output only becomes但是 output 只变成
1003
<select id="Entries_0__StatusId" name="Entries[0].StatusId">
<option value="1001">1001 option 1</option>
<option value="1002">1002 option 2</option>
<option value="1003">1003 option 3</option>
</select>
1002
<select id="Entries_1__StatusId" name="Entries[1].StatusId">
<option value="1001">1001 option 1</option>
<option value="1002">1002 option 2</option>
<option value="1003">1003 option 3</option>
</select>
Eg the 1003 value should also have made option 1003 selected, but nothing is selected .例如,1003 值也应该选择了选项 1003,但没有选择任何内容。
My model is just a simpel array and also contains all values for the dropdownlist.我的 model 只是一个简单的数组,还包含下拉列表的所有值。
public class MyModel
{
public List<MyModelEntry> Entries { get; set; }
public List<SelectListItem> DropDownItems { get; set; }
}
public class MyModelEntry
{
public string StatusId { get; set; }
}
and the populations is also trivial.人口也是微不足道的。
public ActionResult Get(int Id)
{
var model = new MyModel();
model.DropDownItems = new List<SelectListItem>()
{
new SelectListItem { Value = "1001", Text = "1001 option 1" },
new SelectListItem { Value = "1002", Text = "1002 option 2" },
new SelectListItem { Value = "1003", Text = "1003 option 3" }
};
model.Entries = new List<MyModelEntry>()
{
new MyModelEntry { StatusId = "1003" },
new MyModelEntry { StatusId = "1002" }
};
return View(model);
}
Can anyone spot what is wrong.任何人都可以发现什么是错的。 I am not very good at Razor so all suggestions are welcome.
我不太擅长 Razor 所以欢迎所有建议。
In that case, you can use SelectList
:在这种情况下,您可以使用
SelectList
:
@for (int l = 0; l < Model.Entries.Count; l++)
{
@Model.Entries[l].StatusId
@Html.DropDownListFor(m => m.Entries[l].StatusId,
new SelectList(Model.DropDownItems, "Value", "Text", Model.Entries[l].StatusId))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.