Getting Selected text/value using jquery in MVC Razor

 

For more information related to this post click here

For new posts kindly visit our new site http://www.dotnet-helpers.com/

 

Getting Selected text/value using jquery in MVC Razor

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

}

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

View
@Html.DropDownListFor(a => a.SelectedCountry, new SelectList(Model.CountryDetail, “CountryId”, “CountryName”)

Method:1

$(document).ready(function() {

$(“select[id^=’SelectedCountry’]”).change(function() {
alert($(‘option:selected’, $(this)).text());
});
});

Method 2: (using Onchange)

@Html.DropDownListFor(a => a.SelectedCountry, new  …… 

 

For more information related to this post click here

For new posts kindly visit our new site http://www.dotnet-helpers.com/

 

 

Advertisements