当我使用所选的项目不显示DropDownListFor [英] The selected item does not display when I use DropDownListFor
问题描述
我用下面生成一个下拉列表:
I am using the following to generate a drop down list:
@for (var index = 0; index < Model.AdminSummaries.Count(); index++)
{
<div class="rep_tr0">
<div class="rep_td0">
@Html.DropDownListFor(x => Model.AdminSummaries[index].Status,
AdminStatusReference.GetAdminStatusOptions(),
new { id = string.Format("Status_{0}",index ) })
</div>
</div>
}
下面是它生成的HTML:
Here's the HTML it generates:
<select id="Status_1" name="AdminSummaries[1].Status"><option value="1">Released</option>
<option value="2">Review</option>
<option value="3">New</option>
</select>
下面是给出了状态选项的类。
Here's the class that gives the status options.
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptions()
{
return new[]
{
new SelectListItem { Value = "1", Text = "Released" },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
}
}
一切工作不错,除了它不能正确地选择项目。有与'选择'没有选择在AdminSummaries匹配的数据。
Everything works good EXCEPT it doesn't select the items correctly. There's no option with 'selected' to match the data in the AdminSummaries.
怎样才能使它所以都选择了正确的选择列表项?
How can I make it so the correct select list items are selected?
只是为了澄清这一点。我的问题是,如果有与状态的值为3的数据记录,然后,当我看屏幕我看到一个选择列表用释放出。
Just to clarify this. My problem is that if there is a data record with a value of 3 for the status then when I look at the screen I see a select list with the word "Release" showing.
我需要的是选择列表以显示与数据值对应的文本。
What I need is for the select list to show text that corresponds with the data value.
推荐答案
下面是更准确的答案
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptionsFor(AdminSummaries arg)
{
var options = new[]
{
new SelectListItem { Value = "1", Text = "Released" },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
options.First(o=> o.Value == arg).Selected = true;
return options;
}
}
这篇关于当我使用所选的项目不显示DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!