Ajax 调用不是通过 mvc4 中的按钮单击执行的

Ajax call not executed by button click in mvc4

本文关键字:按钮 单击 执行 mvc4 调用 Ajax      更新时间:2023-09-26

>我正在尝试使用 AJAX 按钮单击调用在部分视图中刷新网格。但是按钮的单击方法无法正常工作。

以下代码片段是我用来执行的代码

简讯

 <script  type="text/javascript">
$(document).ready(function () {
    $("#fromDate").datepicker({
        changeMonth: true,
        changeYear: true
    });
    $("#toDate").datepicker({
        changeMonth: true,
        changeYear: true
    });
});
//function buttonClick() {
$("#btnSearch").click($.ajax({
    url: 'Report/SearchGrid',
    type: "POST",
    success: function (html) {}
}));
//}
    </script>

剃须刀代码

<input type="button" value="Search" id="btnSearch" onclick= "buttonClick();" style="width: 90px; border: solid 1px #1570a6; background-color: #1570a6; color: #FFFFFF; font-weight: 100;" />

控制器代码

public ActionResult SearchGrid(ExpenseReportModel model)
{
    ExpenseReportModel expModel = new ExpenseReportModel();
    expModel.ExpenseList = GetExpenseList();
    expModel.ExpenseFromDate = Convert.ToDateTime(model.ExpenseFromDate);
    expModel.ExpenseToDate = Convert.ToDateTime(model.ExpenseToDate);
    return PartialView("_GridData", expModel.ExpenseList);
}

我从您的代码中看不到将数据传递到控制器的位置,或者您在表单上设置任何带有结果的内容。 您的呼叫应如下所示

$('#btnSearch').on('click', function(){
     $.ajax({
         url: "@(Url.Action("Action", "Controller"))",
         type: "POST",
         data: { from: $('.FromDate').val(), to: $('.ToDate').val() },
         cache: false,
         async: true,
         success: function (result) {
             $(".Content").html(result);
         }
     });
});

然后在控制器中,您将接受来自和到的参数,而不是整个模型。您没有在此处进行回发,因此整个模型不会发送回控制器。 如果您有任何问题,请告诉我。