Asp – MVC – Partial Views

asp.net-mvc

I have a Viewpage menu with controller Menucontroller. I have a partialView ViewItems which is strongly typed as object BagItem with contro ller BagItem.
I am trying to render the partial View from View page(Menu.aspx) and can't render partial View. Any help would be greatly appreciated.New to MVC. Here is the code

Menu.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MenuItem>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    OrderMenu
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        OrderMenu</h2>
    <div>
        <table>
            <tr>
                <td>
                    <% List<BagItem> sb = new List<BagItem>(); %>
                    <% Html.RenderPartial("../ShoppingBagItem/ViewItems", sb, (ViewDataDictionary)ViewData["BagItems"]);%>
                </td>
            </tr>
        </table>
    </div>

partialView

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<GuessCafe.Library.BagItem>>" %>
<body>
    <div id="divLatestStocks">
        <%foreach (var item in Model)
          { %>
        <ul>
            <li>
                <%= item.ShoppingBagItemId %>
            </li>
        </ul>

        <%}
        %>
    </div>
</body>

BagItemController

public ActionResult GetShoppingBagItems()
{
    ViewData["BagItems"] = ObjectContext.BagItem.ToList();
    return View(ViewData["BagItems"]);
}

Best Answer

I think what you want is simply this:

<% Html.RenderPartial("ViewItems", ViewData["BagItems"]); %>

This is assuming your partial view is named ViewItems.ascx.

You can remove the following line from your view:

<% List<BagItem> sb = new List<BagItem>(); %>

Update: your partial view should not contain <body>...</body> tags. These should either be on the master page.