MVC Razor : How to get the value of Html.DropDownListFor using controller

In this post, we are going the discuss about how to get the value from the Html. DropDownListFor razor control using a controller.

View

@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 :

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; }
}

Controller :

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)
{

}

OutPut

[

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.