部分视图无法正常工作(整个页面刷新)

3

你好,我刚学习MVC 4,遇到了partialView的问题。我在Google上搜索和学习了很多教程,但是没有找到解决我的问题的方法。我创建了一个PagedList来在Index.cshtml页面上显示记录,然后创建了PartialIndex.cshtml页面来在Partial View中显示记录。 问题在于:当我点击任何页面或导航时,整个页面都会刷新并回传数据,而Partial View则不起作用。我不知道我做错了什么。 我想在PartialIndex.cshtml的DIV中显示表格。

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")
2个回答

3

你最好使用

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

PartialIndex.cshtml的路径是正确的。我猜你指的是视图页面的路径。顺便说一句,我尝试了你建议的解决方案,但它没有起作用。 - Muzamil Rafique
@MuzamilRafique:当您获取页面时,我们可以看到HTML呈现吗?我不知道您的PagedListPager如何工作,因此使用HTML可能会有所帮助。 - clement
是的,我可以看到它。页面呈现得像HTML一样简单,但使用AJAX的部分视图无法正常工作。当我点击任何页面号码时,整个页面都会刷新。 - Muzamil Rafique
请提供HTML代码,如果我看不到Ajax和页面编号,我就无法帮助您! - clement

2

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接