ASP.NET MVC 2 - Html.DropDownList not working in an AJAX form

Posted by Jacob on Stack Overflow See other posts from Stack Overflow or by Jacob
Published on 2010-06-17T18:10:25Z Indexed on 2010/06/17 18:13 UTC
Read the original article Hit count: 869

Filed under:
|
|

I am trying to create an MVC 2 solution and I have run into the following problem:

Index.aspx:

<% using(Ajax.BeginForm("Forms", new AjaxOptions{UpdateTargetId="form", HttpMethod="POST"})) { %>
    <h3>Input: </h3> 
    <p><%= Html.DropDownList("dropDown")%>
    <input type="submit" value="Select Mission" /></p>
<% } %>

HomeController.cs:

    public ActionResult Index()
    {
        var list = new [] { "item1", "item2", "item3" };
        ViewData["dropDown"] = new SelectList(list);
        return View();
    }
    public ActionResult Forms(string dropDown)
    {
        if (dropDown == null || dropDown == "")
            ViewData["txt"] = "Ahhh...";
        else
            ViewData["txt"] = "You entered: " + dropDown;
        return PartialView("Form", dropDown);
    }

Form.ascx:

<%: ViewData["txt"] %>

This does not work. However, the whole thing does work if I use an Html.TextBox instead. For example:

<div id="form">

    <% using(Ajax.BeginForm("Forms", new AjaxOptions{UpdateTargetId="form", HttpMethod="POST"})) { %>
        <h3>Input: </h3> 
        <%= Html.TextBox("textBox") %>
        <input type="submit" value="Select Mission" /></p>
    <% } %>

</div>

(and refactor the method in the controller so that it's argument is textBox instead of dropDown).

My question is why does the AJAX form work for an Html.TextBox, but not for an Html.DropDownList, or what am I doing wrong? My only idea is that maybe the argument in the controller is not supposed to be of type string when using a DropDownList, but this is really just a guess. Thanks in advance.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc