Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 24, 2012 07:50 AM by krokonoster
May 23, 2012 03:26 PM|LINK
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
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
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.