Getting Multiple Selected Values in Html.DropDownlistFor

Use a ListBoxFor instead of DropDownListFor: @Html.ListBoxFor(m => m.branch, CommonMethod.getBranch(“”, Model.branch), “–Select–“) @Html.ListBoxFor(m => m.division, CommonMethod.getDivision(Model.branch, Model.division), “–Select–“) The branch and division properties must obviously be collections that will contain the selected values. And a full example of the proper way to build a multiple select dropdown using a view model: public class MyViewModel { … Read more

Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables

Don’t pass db models directly to your views. You’re lucky enough to be using MVC, so encapsulate using view models. Create a view model class like this: public class EmployeeAddViewModel { public Employee employee { get; set; } public Dictionary<int, string> staffTypes { get; set; } // really? a 1-to-many for genders public Dictionary<int, string> … Read more

Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables

Don’t pass db models directly to your views. You’re lucky enough to be using MVC, so encapsulate using view models. Create a view model class like this: public class EmployeeAddViewModel { public Employee employee { get; set; } public Dictionary<int, string> staffTypes { get; set; } // really? a 1-to-many for genders public Dictionary<int, string> … Read more

Fill drop down list on selection of another drop down list [closed]

Model: namespace MvcApplicationrazor.Models { public class CountryModel { public List<State> StateModel { get; set; } public SelectList FilteredCity { get; set; } } public class State { public int Id { get; set; } public string StateName { get; set; } } public class City { public int Id { get; set; } public int … Read more

onchange event for html.dropdownlist

If you don’t want jquery then you can do it with javascript :- @Html.DropDownList(“Sortby”, new SelectListItem[] { new SelectListItem() { Text = “Newest to Oldest”, Value = “0” }, new SelectListItem() { Text = “Oldest to Newest”, Value = “1” }, new { @onchange=”callChangefunc(this.value)” } }); <script> function callChangefunc(val){ window.location.href = “/Controller/ActionMethod?value=” + val; } … Read more

Populating a razor dropdownlist from a List in MVC

You can separate out your business logic into a viewmodel, so your view has cleaner separation. First create a viewmodel to store the Id the user will select along with a list of items that will appear in the DropDown. ViewModel: public class UserRoleViewModel { // Display Attribute will appear in the Html.LabelFor [Display(Name = … Read more