0

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

查看代码:

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

1 回答 1

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)
于 2012-10-16T08:40:20.837 回答