我有一个MVC Ajax表单。我试图通过更改下拉列表中的项目向控制器发送Ajax请求。我尝试使用下面的代码,但我的控制器正常处理请求而不是Ajax请求。
查看:
@using (Ajax.BeginForm("CashReceipt", "Admin",null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updateContainer" }, new { @class = "smart-form",@id="ajaxform" }))
{
List<SelectListItem> sli = new List<SelectListItem>()
{
new SelectListItem { Text = "Paid", Value = "Paid" },
new SelectListItem { Text = "Unpaid", Value = "Unpaid"
} };
<label class="select">
@Html.DropDownList("Filter", new SelectList(sli, "Text", "Value", ViewBag.Filter), "--", new { @onchange = "this.form.submit();" })
<i></i>
</label>
}
控制器:
[HttpPost]
public ActionResult CashReceipt(string Filter="")
{
if (Request.IsAjaxRequest())
{
return PartialView("_CashReceipt", model);
}
return View(model);
}
Ajax请求在控制器中通过按钮单击事件标识,但不会在更改时显示下拉列表。为什么?请提供一种方法来通过下拉列表项更改来实现此任务。
有人可以帮我解决这个问题吗?
@using (Ajax.BeginForm("CashReceipt", "Admin",null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updateContainer" }, new { @class = "smart-form",@id="ajaxform" }))
{
List<SelectListItem> sli = new List<SelectListItem>()
{
new SelectListItem { Text = "Paid", Value = "Paid" },
new SelectListItem { Text = "Unpaid", Value = "Unpaid"
} };
<button type="submit">Submit</button>
<label class="select">
@Html.DropDownList("Filter", new SelectList(sli, "Text", "Value", ViewBag.Filter), "--", )
/* Here I removed new { @onchange = "this.form.submit();" } */
<i></i>
</label>
}
控制器:
[HttpPost]
public ActionResult CashReceipt(string Filter="")
{
if (Request.IsAjaxRequest())
{
return PartialView("_CashReceipt", model);
}
return View(model);
}
答案 0 :(得分:0)
这对我有用。它可能对某人有帮助。
new { onchange = "$(this.form).submit();" }