asp.net-mvc 带有 Dictionary<string,string> 的下拉列表不适用于所选值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14745028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dropdownlist for with Dictionary<string,string> is not working with selected value
提问by Jitendra Pancholi
I am trying to bind a dropdown in MVC3 in this way.
我正在尝试以这种方式绑定 MVC3 中的下拉列表。
Model
模型
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;
}
Controller
控制器
ViewBag.SexPreference = MemberHandler.SexPreference();
View
看法
@{
var itemsSexPreference = new SelectList(ViewBag.SexPreference, "Value", "Key", Model.sexpreference);
}
@Html.DropDownListFor(m => m.sexpreference, @itemsSexPreference)
Dropdown is not selecting the selected value, don't know why.
下拉没有选择选定的值,不知道为什么。
回答by Darin Dimitrov
Why are you setting ViewBag.SexPreferencewhen you have a model? Forget about this ViewBag. Also you should have 2 properties in order to make a dropdown list: a scalar type property to hold the selected value and a collection property to hold the list of selected values. Right now you seem to be using only one and attempting to bind the DropDown to a collection property which obviously doesn't make any sense.
ViewBag.SexPreference有模型的时候为什么要设置?忘记这个 ViewBag。此外,您应该有 2 个属性来制作下拉列表:一个用于保存所选值的标量类型属性和一个用于保存所选值列表的集合属性。现在您似乎只使用一个并尝试将 DropDown 绑定到一个集合属性,这显然没有任何意义。
Do it the right way, by using a view model:
通过使用视图模型以正确的方式做到这一点:
public class MyViewModel
{
public string SelectedSexPreference { get; set; }
public Dictionary<string, string> SexPreferences { get; set; }
}
that you would populate in your controller action and pass to the view:
您将在控制器操作中填充并传递给视图:
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);
}
and inside your view:
在你的观点中:
@model MyViewModel
@Html.DropDownListFor(
m => m.SelectedSexPreference,
new SelectList(Model.SexPreferences, "Value", "Key")
)
回答by Rubenisme
I did it differently. It's very frustrating this whole thing.
我做了不同的事情。这整个事情非常令人沮丧。
In the controller do something like:
在控制器中执行以下操作:
var percentagesFromDb = _service1.GetPercentagesFromDb(parameters);
var percentages = percentagesFromDb.ToDictionary(percentage => percentage.someKey,
percentage => percentage.someValue);
ViewData["selectedPercentages"] = percentages;
And then in the model:
然后在模型中:
[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");
}
And then finally in the view:
然后最后在视图中:
<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>
回答by SHUBHASIS MAHATA
@* @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!"}})
回答by karaxuna
Model.sexpreference must be one of these values: Straight... This will Work:
Model.sexpreference 必须是以下值之一: Straight... 这将起作用:
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");
}

