C#PartialView返回新页面

时间:2015-03-03 00:53:12

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

在这里遇到问题...当我提交我的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始终存在。

1 个答案:

答案 0 :(得分: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"));
相关问题