0

我有一个 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);
    }
4

1 回答 1

0

这对我有用。它可能对某人有帮助。

new { onchange = "$(this.form).submit();" }
于 2014-07-08T05:27:21.370 回答