How to use a ViewBag to create a dropdownlist?

You cannot used the Helper @Html.DropdownListFor, because the first parameter was not correct, change your helper to: @Html.DropDownList(“accountid”, new SelectList(ViewBag.Accounts, “AccountID”, “AccountName”)) @Html.DropDownListFor receive in the first parameters a lambda expression in all overloads and is used to create strongly typed dropdowns. Here’s the documentation If your View it’s strongly typed to some Model you … Read more

Value cannot be null. Parameter name: items (DrodownList)

Consider the case when model validation fails. View will be redisplayed again with model sent with the request. However this line: new SelectList(ViewBag.Districts, “district_id”, “district_name”, Model.Districts) will have null as a first parameter, since ViewBag.Districts was not repopulated, causing the exception. So in order to avoid exception just set this property again: // If we … Read more

What is the best ways to bind @Html.DropDownListFor in ASP.NET MVC5?

The strongly typed view model approach which does not use dynamic stuff like ViewBag You can add a new property to your view model for the SELECT options of type IEnumrable<SelectListItem>. view model is a simple POCO class used to transfer data between view to action method and vice versa. They are specific to the … Read more

better way to load 2 dropdown in mvc

You approach using ajax is fine although I would recommend a few better practices including using a view model with properties for StateID, CityID StateList and CityList, and using Unobtrusive JavaScript rather than polluting you markup with behavior, and generating the first (“please select”) option with a null value rather than 0 so it can … Read more