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

2 thoughts on “Getting Selected text/value using jquery in MVC Razor

  1. Hello 🙂 Your article is very helpful!
    Is there any way of getting the selected id in JQuery (here id for “INDIA”) and pass it as a var in razor?

    I have made the same functionnality for my app, and I have to pass the id of the selected item to the controller.

    To pass it I need to asign it to a variable declared in razor.
    Can you help?

    Thank you!!!
    Gabriela

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s