I was trying to figure out if it is possible to call from a view a populated dropdownlist.
I try to show an example
From my view now I have this
@Html.DropDownList("AccountId", string.Empty)
and on my controller I have my function
private void PopulateDropDownList(object selectedAccount = null)
{
var accounts = _accountRepository.GetList();
ViewBag.AccountId = new SelectList(accounts, "AccountId", "Description", selectedAccount);
}
But if i use the same dropdown for several different controller I always have to write some code in my controller, instead I would like to create a set of shared dropdownlists that I just can call from my views maybe like this.
cecchin
Member
17 Points
25 Posts
MVC reusable DropDownList
May 23, 2012 03:26 PM|LINK
Hi everybody,
I was trying to figure out if it is possible to call from a view a populated dropdownlist.
I try to show an example
From my view now I have this
@Html.DropDownList("AccountId", string.Empty)and on my controller I have my function
private void PopulateDropDownList(object selectedAccount = null) { var accounts = _accountRepository.GetList(); ViewBag.AccountId = new SelectList(accounts, "AccountId", "Description", selectedAccount); }But if i use the same dropdown for several different controller I always have to write some code in my controller, instead I would like to create a set of shared dropdownlists that I just can call from my views maybe like this.
@Html.AccountDropDownList("AccountId", string.Empty, selectedValue)
and the function automatically return a populated list.
Is this possible? and if it is can I have an example of how implement it?
Thank you very much
Raphael
krokonoster
Contributor
4291 Points
1352 Posts
Re: MVC reusable DropDownList
May 24, 2012 07:50 AM|LINK
This should do the job : http://stackoverflow.com/questions/5052752/adding-your-own-htmlhelper-in-asp-net-mvc-3
You can just call some repository function that return the data you need instead of have it hard coded.