tags:

views:

124

answers:

2

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!

A: 

Use strongly-typed IEnumerable<SelectListItem> - I beleive it is better than SelectList. Try this (did you check the int country parameter in debugger?):

AcceptVerbs(HttpVerbs.Post)]
public ActionResult Info(int country)
{
    ViewData["Countries"] = _db.Countries.Select(x => new SelectListItem {
        Text = x.Name,
        Value = x.Id, // or countryId ?
        Selected = x.Id == country 
    }).ToList();

    ViewData["States"] = _db.States.Where(s => s.countryid == country)
        .Select(x => new SelectListItem {
            Text = x.Name,
            Value = x.Id
        }).ToList();

    // Info? or Intro? (there is only Intro in View)
    ViewData["Info"] = _db.CountryInfo.SingleOrDefault(info => info.countryid == country).Content;

    return View();
}

Hope this helps

eu-ge-ne
Thanks! I'll have a try on strong-typed IEnumerable. As the id part, I'm sure to use the right id. And I can also print out the id and info to the page. Just cann't set the defult. Code:SelectList list = (SelectList)ViewData["States"];<%=list.SelectedValue %>the printed out id is what I want.
Roy
A: 

Are you setting the selectedValue in the SelectList (the second param to the constructor in the below snippet)?

var items = new SelectList(new List<string> {"one", "two", "three"}, "two");
jayrdub
Sure, as you can see from my code. Thanks.
Roy