Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I want to store a selected value of an mvc dropdownlist onchange event in order to use it later.
In the commented code the example with known values of options and their number.
In my dropdown list I don't know in advance the number of value neither their values.
<table width="150" border="0" cellspacing="0" cellpadding="0" style="margin-removed 1px; margin-removed 1px; margin-removed 1px;">
<tr>
           
<td class="formrechinp">
     <%= Html.DropDownList("localsites",
                                          SiteHelper.GetSitesList((CultureInfo) Session["Culture"],
                                                                  menuState.Site_Name),
                                          new {@class = "forminput"}) %>         
                                         
        </td>
    </tr>
</table>     
                                         
        
    


<%--
<select  style="width: 80px;
    border: 1px solid #78B1FF;">
    <option value="<%= PhonebookEP.Helpers.EnumTargetDirectory.Local %>" <% if(PhonebookEP.Helpers.EnumTargetDirectory.Local.Equals(Session["TargetDirectory"])) Response.Write(" selected=\"selected\"");%>>
        <%= ViewRes.SharedStrings.MenuDTLocal %></option>
    
</select>--%>
Posted

1 solution

I understand that you want to use the selected ddl value.
When you run above code the "localsites" ddl will be rendered as elements.
So, the selected value or text can be captured using javascript for the page(rendered) use.

If you want to use the value at server; the quickest way is -
1. Have a post method in controller -
[HttpPost]
ActionResult MyMethod(FormCollection items){...}

2)put your above page source inside the ...your above code..



3)When you click the submit button, it post the data to server then you can retrive the value of ddl from items of MyMethod.

In a couple of weeks I will be posting an article about using jQuery plugin to handle all these postback issues. - sasi
 
Share this answer
 

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