Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a dropdownlist i am able to get the selected id value in controller but how to get the selected value

example
from dropdown i am getting id 1 for country name india
how to get id=1 and contryname=ndia in controler

only id am getting
Posted

The name isn't included in any form submission so if you are using a form to submit the value you'll need to either work the name out from the id that was submitted (do a database lookup for example), or have some javascript on the page that sets the name of the selected item in a hidden field that you then read in your model, or data bind to.

Looking the name up in your server code using the submitted id is the better solution though.
 
Share this answer
 
You can do it this way

HTML
@using (Html.BeginForm( new {id="cf"}))
{
<div>
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td><b>Select a District:</b>
<td><input type="hiden" name="countryname" id="hdnCountryName" /> </td>
</td>
            <td>@Html.DropDownListFor(m => m.countryId, ViewData["countries"] as IEnumerable<SelectListItem>, "Select One")</td>
        </tr>
      
    </table>
</div>
}


c#
C#
public ActionResult GetCountry( string countryname, string countryid)
  {
     //do action

      return View();
  }


javascript
JavaScript
<script type="text/javascript">
$('#countryId').on('change', function(event){
    var form = $("#cf");
$("#hdnCountryName").val($(this).text()) //selected text

    form.submit();
});

</script>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900