C# 为什么我的ajax部分视图呈现在新页面上?

C# 为什么我的ajax部分视图呈现在新页面上?,c#,asp.net,ajax,views,partial-views,C#,Asp.net,Ajax,Views,Partial Views,花了很长时间试图解决这个问题,却一事无成。 基本上,当点击操作链接时,我希望div更新为新页面上显示的值,但似乎无法解决问题。 这是我的密码: @*@model PagedList.IPagedList<S00117372CA3.Product>*@ @*@model Tuple<PagedList.IPagedList<S00117372CA3.Product>, IEnumerable<S00117372CA3.Order>>*@ @model

花了很长时间试图解决这个问题,却一事无成。 基本上,当点击操作链接时,我希望div更新为新页面上显示的值,但似乎无法解决问题。 这是我的密码:

@*@model PagedList.IPagedList<S00117372CA3.Product>*@
@*@model Tuple<PagedList.IPagedList<S00117372CA3.Product>, IEnumerable<S00117372CA3.Order>>*@
@model S00117372CA3.Controllers.ProductViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="searchResult">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().ProductName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().Supplier.CompanyName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().Category.CategoryName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().QuantityPerUnit)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().UnitPrice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().UnitsInStock)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().UnitsOnOrder)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().ReorderLevel)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Products.First().Discontinued)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model.Products) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Supplier.CompanyName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category.CategoryName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.QuantityPerUnit)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UnitPrice)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UnitsInStock)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UnitsOnOrder)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReorderLevel)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Discontinued)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
            @Ajax.ActionLink("Details", "Details", new { id=item.ProductID }, new AjaxOptions { UpdateTargetId = "searchResult", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
        </td>
    </tr>
}

</table>

<div>
    Page @(Model.Products.PageCount < Model.Products.PageNumber ? 0 : Model.Products.PageNumber) of @Model.Products.PageCount
    @if (Model.Products.HasPreviousPage)
    {
        @Html.ActionLink("<<", "Index", new { page = 1})
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "Index", new {page = Model.Products.PageNumber - 1})
    }
    else{
     @:<<
     @Html.Raw(" ");
        @:< Prev   
    }

    @if (Model.Products.HasNextPage)
    {
        @Html.ActionLink("Next >", "Index", new {page = Model.Products.PageNumber + 1})
        @Html.Raw(" "); 
        @Html.ActionLink(">>", "Index", new {page = Model.Products.PageCount})
    }
    else{
     @:Next >
     @Html.Raw(" ")
@:>>   
    }
</div>

<div id="searchResult">

</div>

@Scripts.Render("~/bundles/jquery")
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
@*@model PagedList.IPagedList*@
@*@模型元组*@
@型号S00117372CA3.Controllers.ProductViewModel
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@Html.DisplayNameFor(model=>model.Products.First().ProductName) @DisplayNameFor(model=>model.Products.First().Supplier.CompanyName) @DisplayNameFor(model=>model.Products.First().Category.CategoryName) @DisplayNameFor(model=>model.Products.First().QuantityPerUnit) @DisplayNameFor(model=>model.Products.First().UnitPrice) @DisplayNameFor(model=>model.Products.First().UnitsInStock) @DisplayNameFor(model=>model.Products.First().UnitsOrder) @DisplayNameFor(model=>model.Products.First().ReorderLevel) @DisplayNameFor(model=>model.Products.First().已停止) @foreach(Model.Products中的var项){ @Html.DisplayFor(modelItem=>item.ProductName) @DisplayFor(modelItem=>item.Supplier.CompanyName) @DisplayFor(modelItem=>item.Category.CategoryName) @DisplayFor(modelItem=>item.QuantityPerUnit) @DisplayFor(modelItem=>item.UnitPrice) @DisplayFor(modelItem=>item.UnitsInStock) @DisplayFor(modelItem=>item.UnitsOrder) @DisplayFor(modelItem=>item.ReorderLevel) @DisplayFor(modelItem=>item.Discontracted) @ActionLink(“编辑”,“编辑”,新的{id=item.ProductID})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.ProductID},新的AjaxOptions{UpdateTargetId=“searchResult”,InsertionMode=InsertionMode.Replace,HttpMethod=“GET”})| @ActionLink(“删除”,“删除”,新的{id=item.ProductID}) } @Model.Products.PageCount的@页(Model.Products.PageCount”,“Index”,新的{page=Model.Products.PageCount}) } 否则{ @:下一步> @Html.Raw(“”) @:>> } @Scripts.Render(“~/bundles/jquery”)
文件夹结构中的某个地方。。有一个名为
\u ViewStart.cshtml
的文件。此文件为给定区域内的视图提供初始设置

此文件可能有:

@{
    Layout = "path/to/layout/here.cshtml";
}
在这种情况下。。使用
return View()
渲染的所有视图都将使用此选项

你有两个选择。。要么:

(a)

。。从你的行动方法。或者

b) 在你的部分观点中:

@{
    Layout = null;
}

我猜了一下你的问题。。您的问题并不特别容易理解。

要解决此问题,您只需通过Nuget安装
jquery。不引人注目的ajax
,并将引用添加到视图中,如下所示:

@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
(也可以在捆绑包中执行此操作)


这是必需的,因为微软没有在MVC 5中默认添加此选项。

我的操作方法是这样完成的:返回PartialView(“u Orders”,model);所以我假设这就是你在部分A选项b中的意思。这是其中之一:)也尝试了b选项。仍在打开新的一页。这真让我头疼。/还有其他想法吗?等等。。那么ajax actionlink不是ajaxy?
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")