In this post, we are going the discuss about how to get the value from the Html. DropDownListFor razor control using a controller.
View
1 2 3 4 5 6 7 |
@using (Html.BeginForm("Save", "userdetail", FormMethod.Post, new { @id = "validationlist" })) { @Html.DropDownListFor(model => model.SelectedCountry, new SelectList (Model.CountryDetail.OrderBy(s => s.CountryName), "CountryId", "CountryName",ย Model.SelectedCountry), "-- Select Country --") <input id="submit" type="submit" name="Submit" value="Submit" /> } |
Model :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class UserDetailModel { Display(Name = "CountryDetail")] public List<Country> CountryDetail; [Display(Name = "selectedcountry")] public string SelectedCountry { get; set; } } public class Country { public int CountryId { get; set; } public string CountryName { get; set; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public ActionResult Index() { UserDetailModel obj = new UserDetailModel(); obj.CountryDetail = new List<Country>() { new Country() { CountryId = 1, CountryName =ย "INDIA" }, new Country() { CountryId = 2, CountryName= "US" }, new Country() { CountryId = 3, CountryName= "Russia" }, new Country() { CountryId = 4, CountryName= "UK" } }; return View(obj); } [HttpPost] public void Save(FormCollection form, string selectedCountry) { } |
Leave A Comment