【问题标题】:C# PartialView returns new pageC# PartialView 返回新页面
【发布时间】:2015-03-03 00:53:11
【问题描述】:

这里有一个问题...当我提交我的 Ajax 部分表单(在索引页面上)时,我将 _IndexPartial 页面作为新页面返回(当然没有布局)。

这是索引页面的代码:

@model List<s84.Domain.s84_CustomerProduct>

@{
    ViewBag.Title = "Customer/Product Order";
    Layout = "~/Views/Shared/_NewLayout.cshtml";
}

@using (Ajax.BeginForm("Index", "CustomerProduct", new AjaxOptions { UpdateTargetId = "data-tbl", HttpMethod = "Post" }))
{
    <div class="search">
        @Html.DropDownList("CustomerID",
            new SelectList(s84.Domain.s84_Customer.listItems(), "CustomerID", "CustomerName"))
        <input type="submit" value="Search" />
    </div><br/><br/>
    <div id="data-tbl">
        @Html.Partial("_IndexPartial")
    </div>
}

这是 _IndexPartial 页面的代码:

@model List<s84.Domain.s84_CustomerProduct>

<table class="table">
    <thead>
        <tr>
            <td>&nbsp;</td>
            <td>Product</td>
            <td>Order</td>
            <td>Required Days</td>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                        @Html.ActionLink("Edit", "Edit", new { id = Model[i].CustomerProductID }, null)
                        <text>/</text>
                        @Html.ActionLink("Del", "Delete", new { id = Model[i].CustomerProductID }, null)
                </td>
                <td>@Model[i].s84_Product.ProductName</td>
                <td>@Model[i].ProductOrder</td>
                <td>@Model[i].RequiredDays</td>
            </tr>
        }
    </tbody>
</table>

这是控制器代码:

    [HttpGet]
    public ActionResult Index()
    {
        List<s84_CustomerProduct> lst = s84_CustomerProduct.listItemsByCustomer();

        return View(lst);
    }

    [HttpPost]
    public ActionResult Index(int CustomerID)
    {
        List<s84_CustomerProduct> prod = s84_CustomerProduct.listItemsByCustomer(CustomerID);

        return PartialView("_IndexPartial", prod);
    }

如果我将 Controller Post 方法中的 return PartialView 行改为这个(如下),一切正常:

return PartialView(prod);

我的问题:发生了什么变化?我以前可以返回PartialView(ViewName, Model),但现在它只有在我返回PartialView(Model) 时才有效。为什么会这样?

编辑:我刚刚意识到,当 Post 调用返回 PartialView 时,我也得到了一个查询字符串。每次我发布表格时,我都会被重定向到localhost/CustomerProduct?Length=15。无论我从下拉列表中选择哪个客户,Length=15 始终存在。

【问题讨论】:

  • 您使用的是Ajax.BeginForm() 的哪个重载?好像不太对。 msdn.microsoft.com/en-us/library/…
  • @ataravati 我刚刚将Ajax.BeginForm() 更改为我尝试过的另一个重载,它给了我同样的东西......一个新页面。

标签: c# ajax asp.net-mvc razor


【解决方案1】:

我刚刚弄清楚我改变了什么。我已经停止在我的 jQuery 包中包含 jQuery 验证脚本文件。我将这些 JS 文件重新添加到我的包中...

  • jquery.validate.js
  • jquery.unobtrusive-ajax.js
  • jquery.validate.unobtrusive.js

显然 Razor 或 MVC 的某些部分需要这些文件。这是解决问题的代码:

bundles.Add(new ScriptBundle("~/Content/jquery").Include(
    "~/Scripts/jquery-2.0.3.js",
    "~/Scripts/jquery.validate.js",
    "~/Scripts/jquery.unobtrusive-ajax.js",
    "~/Scripts/jquery.validate.unobtrusive.js"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多