I have following code in my MVC2 view:
<tr class="edit" style="display:none">
<td>
<%= Html.DropDownList("drpFields", new SelectList(Model.Fields, "FieldID", "NiceName", whiteout.FieldID)) %>
</td>
<td>
<%= Html.DropDownList("drpStartTimeh", new SelectList(Model.Hours, whiteout.StartHour.Hour.ToString("0,0")))%>
<%= Html.DropDownList("drpStartTimem", new SelectList(Model.Minutes, whiteout.StartHour.Minute.ToString("0,0")))%>
<%= Html.DropDownList("drpStartTimet", new SelectList(Model.AMPM, whiteout.StartHour.Hour > 12 ? "PM" : "AM"))%>
-
<%= Html.DropDownList("drpEndTime", new SelectList(Model.Hours, whiteout.EndHour.Hour > 12 ? (whiteout.EndHour.Hour - 12).ToString("0,0") : whiteout.EndHour.Hour.ToString("0,0")))%>
<%= Html.DropDownList("drpEndTimem", new SelectList(Model.Minutes, whiteout.EndHour.Minute.ToString("0,0")))%>
<%= Html.DropDownList("drpEndTimet", new SelectList(Model.AMPM, whiteout.EndHour.Hour > 12 ? "PM" : "AM"))%>
</td>
<td>
<%= Html.DropDownList("drprepeat", new SelectList(Model.RepeatList,whiteout.Repeats))%>
</td>
<td>
Active
</td>
<td>
<a class="icon-button-cancel" href='<%: Url.Action("EditWhiteOut", "Settings", new {Id = whiteout.WhiteoutID}) %>'>
<img src='<%: Url.Content("~/static/Images/expanded.png") %>' alt="Delete this device" />
</a>
<a class="icon-button-success" href="#">
<img src="/static/images/gear.png" alt="Edit this device" /></a>
</td>
<td>
</td>
</tr>
I want to create an object of type Whiteout class and populate it with values selected by user from dropdowns and send to settingcontroller’s EditWhiteout action method instead of passing only new {Id = whiteout.WhiteoutID}. How can I do this ?
Please suggest solution.
Thanks.
MVC provides some automatic mapping on form values from the view back into the controller action. Usually you would use a strongly type view i.e. at the top of the page you will have something
like
Control Language="C#" Inherits="System.Web.Mvc.ViewPage<namespace.Whiteout>The names of the dropdown lists need to match those of the properties in the Whiteout class and the controller action will expect a return value of the model type i.e.
The rest should just happen as if by magic. If you are getting confused by the dropdowns it is sometimes easier to replace them with simple inputs until you get the send/receive part working – then change to dropdown.