views:

741

answers:

2

Scenario: I am having a dropdown, a button and html table which displays data from the Model. Below is the code in aspx & controller

Data is displayed without a postback when i click the submit button, but dropdownlist & button appears twice and then the following clicks are fine.

Should i be using Html.RenderPartial anywhere, not sure???

//Controller
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SimpleUpdate(int carMake)
        {
            ViewData["CarMake"] = new SelectList(_carDataContext.Makes.Select(m => new { ID = m.Id, Name = m.Name }), "ID", "Name", carMake);
            var carModel = _carDataContext.Models.Where(m => m.MakeId == carMake).ToList();
            return PartialView("carmodels", carModel);
        }

// in aspx
    <%using (Ajax.BeginForm("SimpleUpdate", new AjaxOptions { UpdateTargetId = "ajaxPanel" }))
          {%>
        <%=Html.DropDownList("CarMake")%>
        <br />
        <input type="submit" value="SimpleUpdate" />
        <%
            }%>
        <div id="ajaxPanel">
            <%
                if (Model != null)
                {
            %>
            <table>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        MakeId
                    </th>
                </tr>
                <%

                    foreach (var item in Model)
                    {%>
                <tr>
                    <td>
                        <%=Html.Encode(item.Id)%>
                    </td>
                    <td>
                        <%=Html.Encode(item.Name)%>
                    </td>
                    <td>
                        <%=Html.Encode(item.MakeId)%>
                    </td>
                </tr>
                <%
                    }%>
            </table>
            <%}%>
        </div>
A: 

Check out this blog post. It should help you get started. It has all of the parts you need, including the database layer, the partial views, and the AJAX submit. It uses a text box instead of a drop down, but the idea is exactly the same.

Jarrett Meyer
A: 

You need to create a partial view(like a user control) not a normal view, in this case. And you have to add your partial view into your main view as a control with Html.RenderPartial.

yapiskan