Hi all,
I'm studing Microsoft ASP MVC framework. Here is something strange of my page.
I have two drop down list on the page. The first one post the form back, passing parameters to the controller. The controller update the second drop down list and other controls, like two text box. I do pass the data to the viewdata. And assigned them to the controls(SelectList to DropDownList, and string to TextBox). But those conrols remains as before the post. How can I fix that? Thanks in advance!
Regards
Edit:
Thanks! Here is my code:
View:
<script type="text/javascript" language="javascript">
function Postback() {
var control = document.getElementById("Country");
var parameter = document.getElementById("CountryName");
parameter.value = control.value;
document.forms.item(0).submit();
}
</script>
<%Html.BeginForm();%>
<fieldset>
<legend>Fields</legend>
<p style="height: 0px">
<input type="hidden" id="CountryName" name="CountryName" value="" />
</p>
<p>
<label for="Country">Country:</label>
<%=Html.DropDownList("Country", (SelectList)ViewData["Countries"]), new { onchange="Postback()" })%>
</p>
<p>
<label for="State">State:</label>
<%=Html.DropDownList("State", (SelectList)ViewData["States"] )%>
</p>
<p>
<label for="Brief Intro">Introduction:</label>
<%= Html.TextBox("Intro", ViewData["Introduction"]) %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<%Html.EndForm(); %>
Controller:
public ActionResult Info()
{
ViewData["Countries"] = new SelectList(_db.Coutries.ToList(), "Id", "Name");
return View();
}
AcceptVerbs(HttpVerbs.Post)]
public ActionResult Info(int country)
{
ViewData["Countries"] = new SelectList(_db.Coutries.ToList(), "Id", "Name", country);
ViewData["States"] = new SelectList(_db.States.Where(s => s.countryid == country).ToList(), "Id", "Name");
ViewData["Info"] = _db.CountryInfo.SingleOrDefault(info => info.countryid == country).Content;
return View();
}
Edit: Call ModelState.Clear() in the second controller fix that problem. Thanks for all of you who offers suggestions! Really Thanks!