Question

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>
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top