C# PartialView returns 新建页面
C# PartialView returns new page
这里有问题...当我提交 Ajax 部分表单(在索引页面上)时,我将 _IndexPartial 页面 returned 作为新页面(没有布局, 当然).
这是索引页的代码:
@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> </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);
我的问题:发生了什么变化?我以前可以 return PartialView(ViewName, Model)
但现在它只在我 return PartialView(Model)
时有效。为什么会这样?
编辑: 我刚刚意识到当 Post 调用 return PartialView 时我也得到了一个查询字符串。每次我 post 表单时,我都会被重定向到 localhost/CustomerProduct?Length=15
。 Length=15
始终存在,无论我从下拉列表中选择哪个客户。
我刚刚弄清楚我改变了什么。我已经停止在 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"));
这里有问题...当我提交 Ajax 部分表单(在索引页面上)时,我将 _IndexPartial 页面 returned 作为新页面(没有布局, 当然).
这是索引页的代码:
@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> </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);
我的问题:发生了什么变化?我以前可以 return PartialView(ViewName, Model)
但现在它只在我 return PartialView(Model)
时有效。为什么会这样?
编辑: 我刚刚意识到当 Post 调用 return PartialView 时我也得到了一个查询字符串。每次我 post 表单时,我都会被重定向到 localhost/CustomerProduct?Length=15
。 Length=15
始终存在,无论我从下拉列表中选择哪个客户。
我刚刚弄清楚我改变了什么。我已经停止在 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"));