I have a DropDownList populated with countries.
When the user picks US I need to show dropdown with US states. If user picks CA - show dropdown with CA provinces...else show a TextBox. Depending on the country picked I hide/show using jQuery. That works ok. The problem is in the ActionResult when user submits.
Html.DropDownList("State", ViewData["States"] as SelectList,
new { @class = "states", @style = "display:none;" })
Html.DropDownList("State", ViewData["Provinces"] as SelectList,
new { @class = "provinces", @style = "display:none;" })
Html.TextBox("State", null, new { @class = "stateFreeForm" })
I pre-load the 2 dropdowns with US and CA data The web model has a string member called State but it is mapping to the first DropDownList (US states). So even though on the page I have a TextBox visible the listing.State member contains the data picked from the US dropdown. I'm using Validation and the State is required so I want to have just one State (not State, Province, Other). Is there something I can do in the jQuery to set change the mapping between web model and control.
I'd prefer to have the the dropdowns pre loaded and just toggle them via scripting rather make an ajax call to reload dropdowns etc.
public ActionResult Post(MyApp.Web.Models.Listing listing)
{
}
Thanks...