在这里遇到问题...当我提交我的Ajax部分表单(在索引页面上)时,我得到_IndexPartial页面作为新页面返回(当然没有布局)。C#部分视图返回新页面
下面是索引页面的代码:
@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
行改为this(below),而不是一切正常:
return PartialView(prod);
我的问题:改变了什么?我曾经能够返回PartialView(ViewName, Model)
,但现在只有当我返回PartialView(Model)
时才起作用。这是为什么发生?
编辑:我刚刚意识到我也收到一个查询字符串,当邮政调用返回PartialView。每次我发布表单时,我都会重定向到localhost/CustomerProduct?Length=15
。无论我从下拉列表中选择哪个客户,Length=15
都会一直存在。
你正在使用哪个'Ajax.BeginForm()'重载?这看起来不正确。 https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.beginform(v=vs.118).aspx – ataravati 2015-03-03 01:03:26
@ataravati我刚刚改变了'Ajax.BeginForm( )'对另一个超载我试过了,这给了我同样的东西......一个新的页面。 – Targaryen 2015-03-03 01:05:26