【问题标题】:PartialView is not working (whole page refresh instead)PartialView 不起作用(改为刷新整个页面)
【发布时间】:2015-02-08 13:02:53
【问题描述】:

您好,我是 MVC 4 的新手。我在使用 partialView 时遇到问题。我对其进行了很多搜索并研究了不同的教程,但找不到任何可以解决我的问题的解决方案。我创建了一个 PagedList 来显示记录在 Index.cshtml 页面。然后我创建了 PartialIndex.cshtml 页面以在 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")

【问题讨论】:

    标签: ajax asp.net-mvc-4 pagedlist ispostback


    【解决方案1】:

    你应该更好地使用

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

    【讨论】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 2015-02-16
      • 2022-01-16
      相关资源
      最近更新 更多