Home > DeveloperSection > Forums > How to get id from dropdownlist?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    January-22-2014 11:54 PM

 C# C# 
Ratings:


 1 Reply(s)
 701  View(s)
Rate this:

I have such list

public IList<SelectListItem> ToCountryList { get; set; }

I have such line in the razor view

@Html.DropDownListFor(m => m.Filter.ToCountryId, Model.ToCountryList, string.Empty, new { style = "min-width: 100px; width:180px" })

Now i want with juery to select ToCountryId from the selected value of dropdownlist .How to get it ?

Such thing gives nothing

$("select[name='Filter.ToCountryId'] option:selected").val()

html from browser (not sure how to publush - so plain text)

..select starts

select id="Filter_ToCountryId" name="Filter.ToCountryId" style="min-width: 100px; width:180px"

option tags with countries

..select ends

Guys, I need an id of the record.

For example - I have a pair ID-NAME 5-USA. So i need to retrieve 5 (int)



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-23-2014 12:59 AM

Hi Ankita,

Just give your dropdown a deterministic id:

@Html.DropDownListFor(

    m => m.Filter.ToCountryId,

    Model.ToCountryList,

    string.Empty,

    new { id = "myddl", style = "min-width: 100px; width:180px" }

)

and then you could easily get the selected value using an id selector:

$('#myddl').val()


Don't want to miss updates? Please click the below button!

Follow MindStick