MVC3 Customizing DropdownList items in strongly typed view?

Html. DropDownList has another overload, namely.

Up vote 0 down vote favorite share g+ share fb share tw.

I have an auto generated DropDownList from Entity Framework in a strongly typed view: @Html. DropDownList("User_FK", String. Empty) @Html.

ValidationMessageFor(model => model. User_FK) Here is the action code: public ActionResult Create() { ViewBag. SystemMaster_FK = new SelectList(db.

SystemMasters, "System_PK", "Name"); ViewBag. User_FK = new SelectList(db. Users, "User_PK", "NetworkLogin"); return View(); } I need this list to display the names of people loaded from Active Directory.

How do I customize the select list options seperately? Entity-framework asp.net-mvc-3 link|improve this question edited Jun 10 '11 at 14:00 asked Jun 10 '11 at 12:34Tjaart332210 83% accept rate.

Html. DropDownList has another overload, namely: public static MvcHtmlString DropDownList( this HtmlHelper htmlHelper, string name, IEnumerable selectList ) So, you specify a name string and then an IEnumerable generated any way you want. So, (of course, I'm assuming some types here--MyUserType, MyActiveDirectoryRepository) List users = new List(); foreach(var user in MyActiveDirectoryRepository.GetUsers()) { users.

Add(new MyUserType() { ADName = user. Username, ID = user.ID // or SAM token, or something similar }); } Pass the list to your view via View Model pattern ViewBag public ActionResult Create() { // populate list as above ViewBag. User_FK = new SelectList(users, "ID", "ADName"); return View(); } Then, after passing it to your view (preferably, via view model not ViewBag) @Html.

DropDownList("NameOfDropDown", ViewBag. User_FK).

CreateViewModel() doesn't exist or I don't know where to find it. Is there something I'm missing? – Tjaart Jun 10 '11 at 13:09 @Tjaart Sorry--I forgot to mention that in my list of assumed types.

It's a plain class containing a List property named MyList--it's nothing more than a means of wrapping the model sent to your view. Hence, "View Model" – David Jun 10 '11 at 13:11 It doesn't see the MyList in my view. Maybe because my model is already defined in my view as @model SystemInformationManager.Models.

SystemMaintainer – Tjaart Jun 10 '11 at 13:22 @Tjaart I assumed there was no model according to your Create action method. Return View(); doesn't send a model to the view. Are you able to get inside SystemMaintainer?

If not, you may have to wrap it in CreateViewModel and modify any existing view code that uses the SystemMaintainer properties. – David Jun 10 '11 at 13:27 Ok. I got it working.

I had to change the selectlist in the controller action to ViewBag. User_FK = new SelectList(users, "ID", "ADName"); where users is my custom user type. The reason an object isn't passed to the view is that the Create is loading the form to insert into the SystemMaintainer table.

SystemMaintainer is accessible but as a new object. Are you willing to edit your answer to reflect what works. I will change the question to make it clear that it is a strongly typed view.

Thank you! – Tjaart Jun 10 '11 at 13:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions