通过$ ajax将数据从视图发送到控制器时出错

时间:2014-04-29 13:48:20

标签: jquery ajax post

我通过jquery ajax调用将数据从视图发送到控制器但是这个给出错误对话框我不知道错误是什么。我的代码。 请帮帮我。我试过评论的alernative,也试过这个

代码:

          data:{'startValue' : startValue,'endValue':endValue },
          data:{"startValue" : startValue,"endValue":endValue },

他们都没有工作。

预订模式:

     public class Booking
      {

    public string reservationId { get; set; }
    public string startDate { get; set; }
    [Required(ErrorMessage = "Please select Start date")]
    public string endDate { get; set; }
    [Required(ErrorMessage="Please select end date")]
    public string startHour { get; set; }
    [Required(ErrorMessage="Please select Start time")]
    public string endHour { get; set; }
    [Required(ErrorMessage="Please select end time")]
    public string amount { get; set; }
    public string vehicleId { get; set; }
    public string spaceId { get; set; }
    public string userId { get; set; }
    }

查看:

       <script type="text/JavaScript">
        function sendVales() {
        var startValue = $('#startDate').val();
        var endValue = $('#endDate').val();
        alert(startValue + "" + endValue);

        var dataRequested = { startDate: startValue, endDate: endValue };
        $.ajax({

            type: "POST",
            contentType: "application/json;charset=utf-8",
            url: '@Url.Action("makeReservation", "Booking")',
          //  data:{startValue : startValue,endValue:endValue },
            data: JSON.stringify(dataRequested),
                dataType: "json",
                success: function (data) {
                    alert("Success, sent data to controller");
                },
                error: function (data) {
                    alert("Error: " + data.responseText);
                }
        });
    }

</script>

控制器

       [HttpPost]

        public JsonResult makeReservation(Booking bookModel)
        {    bool success = false;
        try
        {
            success = bookingObj.addBooking(userName, bookModel);
            if (success)
            {
                ModelState.AddModelError("", "Space has been booked.");
            }
            else
            {
                ModelState.AddModelError("", "Space can be reserved.Correct the errors and try aggain");
            }

        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex);
        }
             return Json(new { success }, JsonRequestBehavior.AllowGet);
         }

0 个答案:

没有答案