3

Hello I am new to MVC 4. I am having a problem with partialView.. I goole it and searched alot and studied different tutorials but couldn't find any solution that can address my problem.I created a PagedList to display records at Index.cshtml page. Then I created PartialIndex.cshtml page to display records in Partial View. Here is problem: When I click on any page number or navigate.. whole page refreshes and post back... partial view is not working.Don't Know where I am doing wrong. I want to show table inside the DIV in PartialIndex.cshtml

PartialIndex.cshtml:

<div id="targetContainer"> //I want to show this DIV in partial view.
<table>
    <tr>
        <th>
            @Html.ActionLink("ID", "Index", new { sortOrder = ViewBag.customerID, currentFilter=ViewBag.CurrentFilter })
        </th>
        <th>
        </th>
        <th>
            @Html.DisplayName("First Name")
        </th>
        <th></th>
        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.lName, currentFilter=ViewBag.CurrentFilter })
        </th>
        <th></th>
        <th>
            @Html.DisplayName("Contact Num")
        </th>

        <th>
            @Html.DisplayName("Address")
        </th>

        <th>
            @Html.DisplayName("Email")
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.customerId)
        </td>
        <td>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.fName)
        </td>
        <td>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.lName)
        </td>
        <td>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.contactNum)
        </td>
        <td>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.address)
        </td>
        <td>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.email)
        </td>
        <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID })
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>

    </tr>
}
</table>
</div>

<br />
       Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

            @Html.PagedListPager(Model, page => Url.Action("Index", 
             new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(
                                       new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", UpdateTargetId = "targetContainer" }))

Index.cshtml:

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />


@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("PartialIndex")
clement
  • 4,204
  • 10
  • 65
  • 133
Muzamil Rafique
  • 147
  • 1
  • 12

2 Answers2

3

You should better use

Html.RenderPartial("~/Views/Shared/_Yourpartial.cshtml")
clement
  • 4,204
  • 10
  • 65
  • 133
  • path to PartialIndex.cshtml is correct. I guess you are pointing towards path of view page. BTW i tried your suggested solution but it didn't work. – Muzamil Rafique Dec 10 '14 at 11:47
  • @MuzamilRafique: can we see the html rendered when you get the page? I don't know how your PagedListPager works so with html it will probably helps – clement Dec 10 '14 at 12:26
  • yes I can see it. Page renders simple like html but partial view with AJAX doesn't work. Whole page refreshes when i click on any page number. – Muzamil Rafique Dec 10 '14 at 12:58
  • give html please I can't help you if I can't see Ajax and page numbers! – clement Dec 10 '14 at 13:01
2

Html.RenderPartial("~/Views/Shared/PartialIndex.cshtml")

piet.t
  • 11,718
  • 21
  • 43
  • 52
Farrukh Liaqat
  • 116
  • 1
  • 1
  • 11