MVC Ajax请求在提交请求时不在控制器中触发

时间:2013-09-11 23:54:26

标签: asp.net-mvc asp.net-ajax

我是新手,不知道为什么它没有到达我的控制器。我输入一个ID,没有任何反应,控制器中的断点没有被击中。我错过了什么吗?

CONTROLLER~ / Controllers / AppointmentController.cs

[HttpPost]
        public ActionResult Schedule(int id = 0)
        {
            if (Request.IsAjaxRequest())
            {
                Customer customer = _customerRepository.Find(id);
                return PartialView("Schedule", customer); 
            }
            return View();
        }

查看〜/ Views / Appointment / Schedule.cshtml

@model Customer
@{
    ViewBag.Title = "Schedule";
}

<h2>Schedule</h2>

@using (Ajax.BeginForm("Schedule", "Appointment",
    new AjaxOptions()
{
    HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "Customer"
}))
{
    <label>ID Search</label>
    <input type="search" name="customerSearch" placeholder="ID" />
    <input type="submit" value="Continue" />
}
<div id="Customer">
    @Html.Partial("~/Views/Customer/_Customers.cshtml", Model)
</div>

1 个答案:

答案 0 :(得分:0)

您的行动标有[HttpPost]。

这对于使用POST HttpMethod的AJAX请求可以正常工作,但初始页面加载将是一个GET请求 - 尝试删除[HttpPost]或重组,以便您拥有:

    [HttpPost]
    public ActionResult Schedule(int id = 0, string customerSearch = "")
    {
        Customer customer = _customerRepository.Find(id);
        return PartialView("Schedule", customer); 
    }

    [HttpGet]
    public ActionResult Schedule(int id = 0)
    {
        return View();
    }

此外,您的AJAX表单不是从name =“id”的输入发布的,因此需要解决。

相关问题