我希望使用ASP.NET MVC将字符串发送到我的家庭控制器。 我正在使用Ajax调用执行此操作,但是在发送字符串时我也需要更改视图。
问题是,为了更改页面,我们需要在Ajax成功处理程序中执行此操作。这导致我们再次调用控制器方法。但是,这次日期字符串为null,导致null异常。我们如何解决这个问题?
我们的AJAX电话:
dayClick: function(date) {
$.ajax({
url: '/Home/Booking',
data: {'selectedDate' : date.format()},
type: "get",
cache: false,
success: function (result) {
window.location.href = "/Home/Booking"
},
error: function (error) {
console.log(error.message);
}
});
}
我们的控制器方法:
public IActionResult Booking(string selectedDate)
{
var booking = new Booking();
DateTime selectedDatetime = DateTime.ParseExact(selectedDate, "yyyy-MM-dd", CultureInfo.CurrentCulture);
booking.Date = selectedDatetime;
var viewModel = new BookingsideViewModel
{
Subjects = new[] {"Matematik", "Dansk", "Engelsk"},
Booking = booking
};
return View(viewModel);
}
答案 0 :(得分:3)
在成功处理程序中再次调用controller方法时,我们再次需要使用url
传递日期List<XElement> entries = elements
.Descendants(childNode)
.Where(child => child.Value == childNodeValue)
.SelectMany(child => child.Ancestors(parentNode))
.ToList();
答案 1 :(得分:2)
你应该把它作为查询参数发送到URL,如下所示:
window.location.href = "/Home/Booking?selectedDate=" + date.format()