我试图以这种方式绑定MVC3中的下拉列表。
模型
public static Dictionary<string, string> SexPreference()
{
var dict = new Dictionary<string, string>();
dict.Add("Straight", "S");
dict.Add("Gay", "G");
dict.Add("Bisexual", "BISEX");
dict.Add("Bicurious", "BICUR");
return dict;
}
控制器
ViewBag.SexPreference = MemberHandler.SexPreference();
查看
@{
var itemsSexPreference = new SelectList(ViewBag.SexPreference, "Value", "Key", Model.sexpreference);
}
@Html.DropDownListFor(m => m.sexpreference, @itemsSexPreference)
下拉列表未选择所选值,不知道原因。
答案 0 :(得分:20)
为什么在拥有模特时设置ViewBag.SexPreference
?忘了这个ViewBag。此外,您还应该有2个属性才能生成下拉列表:标量类型属性用于保存选定值,集合属性用于保存选定值列表。现在你似乎只使用了一个并尝试将DropDown绑定到一个显然没有任何意义的集合属性。
使用视图模型以正确的方式执行:
public class MyViewModel
{
public string SelectedSexPreference { get; set; }
public Dictionary<string, string> SexPreferences { get; set; }
}
您将在控制器操作中填充并传递给视图:
public ActionResult SomeAction()
{
var model = new MyViewModel();
// Set the value that you want to be preselected
model.SelectedSexPreference = "S";
// bind the available values
model.SexPreferences = MemberHandler.SexPreference();
return View(model);
}
并在您的视图中:
@model MyViewModel
@Html.DropDownListFor(
m => m.SelectedSexPreference,
new SelectList(Model.SexPreferences, "Value", "Key")
)
答案 1 :(得分:0)
Model.sexpreference必须是以下值之一:直... 这将有效:
public static Dictionary<string, string> SexPreference()
{
var dict = new Dictionary<string, string>();
dict.Add("S", "Straight");
dict.Add("G", "Gay");
dict.Add("BISEX", "Bisexual");
dict.Add("BICUR", "Bicurious");
return dict;
}
@{
var itemsSexPreference = new SelectList(ViewBag.SexPreference, "Key", "Value", "S");
}
答案 2 :(得分:0)
我做得与众不同。这件事非常令人沮丧。
在控制器中执行以下操作:
var percentagesFromDb = _service1.GetPercentagesFromDb(parameters);
var percentages = percentagesFromDb.ToDictionary(percentage => percentage.someKey,
percentage => percentage.someValue);
ViewData["selectedPercentages"] = percentages;
然后在模型中:
[Display(Name = "%")]
public string Percentage { get; set; }
private static Dictionary<string, string> GetPercentageList()
{
return new Dictionary<string, string>
{
{"100", "100%"},
{"90", "90%"},
{"80", "80%"},
{"70", "70%"},
{"60", "60%"},
{"50", "50%"},
{"40", "40%"},
{"30", "30%"},
{"20", "20%"},
{"10", "10%"}
};
}
public SelectList GetSelectList(string selected, object selectedPercentages)
{
var selectedDict = new Dictionary<string, string>();
if (selectedPercentages != null)
selectedDict = (Dictionary < string, string>) selectedPercentages;
var selectedvalue = selectedDict.ContainsKey(selected) ? selectedDict[selected] : "100";
Percentage = selectedvalue;
return new SelectList(GetPercentageList(), "Key", "Value");
}
最后在视图中:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>@Html.LabelFor(m => m.Items, new {@class = "control-label"})</th>
<th>@Html.LabelFor(m => m.Percentage, new {@class = "control-label"})</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr>
<td>
@item.Value
</td>
<td>
@Html.DropDownListFor(m => m.Percentage,
Model.GetSelectList(item.Key, ViewData["selectedPercentages"]),
new {@class = "form-control", id = item.Key})
</td>
</tr>
}
</tbody>
</table>
答案 3 :(得分:0)
@* @Html.DropDownListFor(x => x.GetAttribute_ID, new SelectList(Model.GetAttributes, "ID", "AttributeName", Model.GetAttribute_ID), "Select Attribute", new Dictionary<string, object>() { }) new { @id = "SelectAttribute", @name = "SelectAttribute[]", @class = "form-control dd" })*@
@Html.DropDownListFor(x => x.GetAttribute_ID, new SelectList(Model.GetAttributes, "ID", "AttributeName", Model.GetAttribute_ID), "Select Attribute", new Dictionary<string, object>() { { "class", "skill-level" }, { "data-val", "true" }, { "data-val-required", "Select skill level!"}})