简体   繁体   English

在MVC4中,单击按钮未执行Ajax调用

[英]Ajax call not executed by button click in mvc4

I am trying to refresh grid in partial view using AJAX call of button click. 我正在尝试使用按钮单击的AJAX调用来刷新部分视图中的网格。 But click method of button is not working properly. 但是按钮的点击方法无法正常工作。

Following code snippet is code i am using to execute 以下代码段是我正在执行的代码

JQUERY JQUERY

 <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>

Razor Code 剃刀代码

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

Controller Code 控制器代码

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);
}

I don't see from your code where you are passing data to the controller or where you are setting anything on your form with the result. 从您的代码中看不到您正在向控制器传递数据或在表单上设置任何结果的地方。 Your call should look something like this 您的通话应该看起来像这样

$('#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);
         }
     });
});

then in your controller you will accept parameters from and to instead of the whole model. 然后在您的控制器中,您将接受从和到的参数,而不是整个模型。 You aren't doing a post back here so the whole model won't be sent back to the controller. 您无需在此处进行发布,因此不会将整个模型发送回控制器。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM