问题描述:

I'm having a mvc form which among other things contains an int that should be mapped to text strings displayed in a dropdown.

MyViewModel

{

...other fields...

int Level { get; set; }

...other fields...

}

Is it possible to use RenderAction for the Level field and generate the dropdown from a separate action?

I hope this question is coherent, I'm quitting coffee and my head isn't where it usually is.

网友答案:

Although many people will advise (rightly so) not to do this but if you insist you can do something like

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

as you want to reuse this dropdown you can accept its html name attribute as action method parameter

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);

}

in your view you can write something like

<:Html.Dropdown(Model.InputName, Model.Items)%>
相关阅读:
Top