将jquery参数传递给Controller Action

时间:2012-10-16 08:35:31

标签: jquery asp.net-mvc asp.net-mvc-3

我想将带有ajax函数的2个参数传递给Controller Action,但是我使用它的代码不起作用。 调用该动作但没有参数;下面的代码有什么问题?请帮忙。 谢谢!

查看代码:

        <fieldset>
            @Html.Label("Start date: *")
            @Html.TextBoxFor(model => model.startDate, new { Class = "calendar", data_mini = true, data_role = "datebox", data_options = "{\"mode\":\"calbox\",\"useNewStyle\": true,\"overrideStyleClass\": \"calendar\",\"fieldsOrderOverride\": [\"d\",\"m\",\"y\"],\"headerFormat\":\"%A,%d, %-m, %Y\",\"dateFormat\":\"DD-MM-YYYY\",\"useTodayButton\":true}" })
        </fieldset>
        <fieldset>
            @Html.Label("End date: *")
            @Html.TextBoxFor(model => model.endDate, new { Class = "calendar", data_mini = true, data_role = "datebox", data_options = "{\"mode\":\"calbox\",\"useNewStyle\": true,\"overrideStyleClass\": \"calendar\",\"fieldsOrderOverride\": [\"d\",\"m\",\"y\"],\"headerFormat\":\"%A,%d, %-m, %Y\",\"dateFormat\":\"DD-MM-YYYY\",\"useTodayButton\":true}" })
        </fieldset>
        <fieldset>
            @Html.Label("Submited: *")
        </fieldset>
        <a data-role="button" id="search" name="search" data-theme="a" class="book ui-btn ui-btn-corner-all ui-shadow ui-btn-up-a">
        <span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Search</span></span></a>

Jquery代码:

<script type="text/javascript">
 $("#search").click(function () {
    GetNewDaysList();
   });
//gets elements for the selected dates
function GetNewDaysList() {
var action = '@Url.Action("GetNewDays", "ApproveWork")';
var criteria = new Array($("#startDate").val(),$("#endDate").val());
    var opt = {
        type: "GET",
        data: { criteria: criteria },
        url: action,
        success: DaysListSuccess,
        error:DaysListFailed
    };
    jQuery.ajax(opt);
}

  function DaysListSuccess(data) {
    if (data != undefined) {
        $.mobile.hidePageLoadingMsg();
        $('ul').hide();
        $("#employeeDays").html(data).trigger('create');
        $("#searchSection").show();
        $("#employeeDays").show();
    }
}

  function DaysListFailed() {
 //
}
});

控制器代码:

    [HttpGet]
      public ActionResult GetNewDays(IEnumerable<string> criteria)
    {
        List<string> criteriaList = criteria.ToList();
        DateTime start = DateTime.ParseExact(criteriaList[1],"dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
        string startToString = start.ToString("yyyyMMdd");
        DateTime end = DateTime.ParseExact(criteriaList[2], "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
        string endToString = end.ToString("yyyyMMdd");            
        List<string> daysList = new List<string>();
       var model = new ApproveWorkModel();
       model.daysList = daysList;
       return PartialView ( "EmployeeDays",model);
    }
}

1 个答案:

答案 0 :(得分:1)

使用它:

var opt = {
    type: "GET",
    data: { 
            startDate:  $("#startDate").val(),
            endDate: $("#endDate").val()
    },
    url: action,
    success: DaysListSuccess,
    error:DaysListFailed
};

并在控制器中:

public ActionResult GetNewDays(string startDate, string endDate)