11

我正在尝试以这种方式绑定 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)

下拉菜单没有选择选定的值,不知道为什么。

4

4 回答 4

28

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")
)
于 2013-02-07T07:00:05.247 回答
1

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");
}
于 2013-02-07T06:40:06.893 回答
1

我做了不同的事情。这整件事非常令人沮丧。

在控制器中执行以下操作:

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>
于 2017-05-08T18:53:59.037 回答
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!"}})
于 2019-02-18T21:04:20.803 回答