MrM
MrM

Reputation: 21999

How do I send data from DropDownList to Html.BeginForm in MVC?

I would like to pass a date from a drop down list to my ID in the Html.BeginForm. This is what I have so far.

<%using (Html.BeginForm("Inquiry", "Billing", new { Date = ????? }, FormMethod.Post))


        <select id="myList" name="myList" >
        <option>Term - Balance</option>
        foreach (var item in Model.Term)
          { %>                             
        <option value="/<%=String.Format("{0:yyyyMMdd}", item.Date) %>" >
        <%=item.Date.ToShortDateString()%> - <%=String.Format("{0:C}", (item.Balance))%></option>
        <% } %>            
        </select>

Any suggestions?

Upvotes: 0

Views: 879

Answers (1)

womp
womp

Reputation: 116987

You can add additional attributes to the BeginForm method like this:

 <% using (Html.BeginForm("Inquiry", "Billing", 
                             FormMethod.Post, new { id = myDate.ToString() }))
       { %>

...

    <%} %>

If you're asking how to make the form ID change when a user changes the selection on a dropdownlist on the form, then you'll need to use javascript for that. The code above only runs once at render time, so if your myDate variable isn't set by then, then you can't get it on the server side.

Upvotes: 1

Related Questions