Note that there are some explanatory texts on larger screens.

plurals
  1. POControls not refreshed in ASP.net MVC
    primarykey
    data
    text
    <p>I'm studing Microsoft ASP MVC framework. Here is something strange of my page.</p> <p>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!</p> <p>Regards</p> <p>Edit:</p> <p>Thanks! Here is my code:</p> <p>View:</p> <pre><code> &lt;script type="text/javascript" language="javascript"&gt; function Postback() { var control = document.getElementById("Country"); var parameter = document.getElementById("CountryName"); parameter.value = control.value; document.forms.item(0).submit(); } &lt;/script&gt; &lt;%Html.BeginForm();%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p style="height: 0px"&gt; &lt;input type="hidden" id="CountryName" name="CountryName" value="" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Country"&gt;Country:&lt;/label&gt; &lt;%=Html.DropDownList("Country", (SelectList)ViewData["Countries"]), new { onchange="Postback()" })%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="State"&gt;State:&lt;/label&gt; &lt;%=Html.DropDownList("State", (SelectList)ViewData["States"] )%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Brief Intro"&gt;Introduction:&lt;/label&gt; &lt;%= Html.TextBox("Intro", ViewData["Introduction"]) %&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;%Html.EndForm(); %&gt; </code></pre> <p>Controller:</p> <pre><code>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 =&gt; s.countryid == country).ToList(), "Id", "Name"); ViewData["Info"] = _db.CountryInfo.SingleOrDefault(info =&gt; info.countryid == country).Content; return View(); } </code></pre> <p>Edit: Call ModelState.Clear() in the second controller fix that problem. Thanks for all of you who offers suggestions! Really Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload