1

我有一个 mvc 表单,其中包含一个应该映射到下拉列表中显示的文本字符串的 int。

MyViewModel
{
    ...other fields...
    int Level { get; set; }
    ...other fields...
}

是否可以将 RenderAction 用于 Level 字段并从单独的操作生成下拉列表?

我希望这个问题是连贯的,我要戒掉咖啡了,我的头不在通常的地方。

4

1 回答 1

1

虽然很多人会建议(正确地)不要这样做,但如果你坚持你可以做类似的事情

public VMDropDown
{
     IEnumerable<SelectListItem> Items{get;set;}
     public string InputName{get;set;}
}

当您想重用此下拉列表时,您可以接受其 html 名称属性作为操作方法参数

public ActionResult(string inputName)
{
    VMDropDown model = new VMDropDown();
    model.InpuName = inputName;
    model.Items = //populate Text and Value property of each SelectListItem from db
    return View(model);

}

在您看来,您可以编写类似的内容

<:Html.Dropdown(Model.InputName, Model.Items)%>
于 2011-04-20T15:48:30.703 回答