【发布时间】: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> </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