Asp.net mvc 提交请求时控制器中未触发MVC Ajax请求

Asp.net mvc 提交请求时控制器中未触发MVC Ajax请求,asp.net-mvc,asp.net-ajax,Asp.net Mvc,Asp.net Ajax,我是新手,不知道为什么它没有到达我的控制器。我输入了一个ID,但什么也没发生,我在控制器中的断点没有被击中。我错过什么了吗 CONTROLLER~/Controllers/AppointmentController.cs [HttpPost] public ActionResult Schedule(int id = 0) { if (Request.IsAjaxRequest()) {

我是新手,不知道为什么它没有到达我的控制器。我输入了一个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();
        }
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>
@model客户
@{
ViewBag.Title=“时间表”;
}
日程
@使用(Ajax.BeginForm(“时间表”、“约会”),
新的AjaxOptions()
{
HttpMethod=“POST”,
InsertionMode=InsertionMode.Replace,
UpdateTargetId=“客户”
}))
{
ID搜索
}
@Html.Partial(“~/Views/Customer/\u Customers.cshtml”,模型)

您的操作标有[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”的输入中发布的,因此需要解决这个问题。

您是否将引用设置为jquery或jquery不引人注目?您是否在使用的任何浏览器中查看javascript控制台,以查看是否有任何JS错误发生?此外,如果使用开发人员工具中的Chrome use Network tab查看正在完成的帖子及其返回的结果(或使用Fiddler监视请求)。Unobtrusive在Web.Config中设置为false,并从脚本包中删除。控制台中没有错误,看起来它只是在进行一次定期提交,并重新加载整个页面。谢谢!问题在于我的id变量。我按照您的建议将其更改为customerSearch,结果成功了!:D