jQuery 调用时 RedirectToAction 不起作用?
我试图通过 jQuery 调用操作方法,并在此处获得传递参数的帮助: 使用 jQuery 在 MVC 中调用操作方法且参数不起作用 。但是,即使现在参数已正确发送,在操作方法中我需要重定向(传递相同的参数)到另一个操作方法。在调试器中,我可以看到重定向已执行,并且参数仍然存在,但返回的视图没有相应更新...
调用使用 jQuery 重定向的操作方法是否存在问题?
jQuery 调用的操作方法:
public ActionResult SelectDate(string date)
{
DateTime dateObject = DateTime.Parse(date);
MyCalendar calendar = new MyCalendar();
string number = calendar.GetWeekNumber(dateObject).ToString();
string year = dateObject.Year.ToString();
return RedirectToAction("Edit", new { number = number, year = year });
}
操作方法重定向到:
public ActionResult Edit(string number, string year) //Why string here???
{
int n;
if (string.IsNullOrEmpty(number))
n = myCalendar.GetWeekNumber(DateTime.Today);
else
n = Int32.Parse(number);
int y;
if (string.IsNullOrEmpty(year))
y = DateTime.Today.Year;
else
y = Int32.Parse(year);
List<Customer> customers = _repository.Customers;
ViewData["Customers"] = new SelectList(customers, "CustomerId", "CustomerName");
ViewData["WeekNumber"] = n;
ViewData["Year"] = y;
return View();
}
或者 jQuery get() 不是调用操作方法加载新页面的正确方法?请注意,使用 ActionLink 执行相同的操作效果很好,只是我需要通过 jQuery 调用它,因为我使用 datepicker 插件和按钮作为调用操作的方式。
我做错了什么?
更新:
实际上,RedirectToAction 似乎不是问题,我修改了代码,以便 jQuery 现在直接调用最终操作方法(通过直接在 jQuery 中执行第一个操作方法的工作,即将日期转换为周)和年份)。但我仍然没有得到新的一周和一年的新页面。
这是新的 jQuery:
function selectWeek() {
$('#selectWeekButton').click(function (event) {
var date = $('#selectWeekId').val();
var selectedDate = new Date(date);
var year = selectedDate.getFullYear();
var week = selectedDate.getWeekOfYear();
var url = '<%: Url.Action("Edit") %>';
$.get(url, { number: week, year: year }, function (data) {
// alert('Test');
});
});
}
同样,它使用的 get 是否不正确?我不想在 html 标签中加载内容,我只想使用 jQuery 做与 actionlink 完全相同的事情,即调用一个 action 方法来获取编辑视图...
例如,这个 actionlink 工作正常,调用一个名为 Next 的操作方法,它重定向到编辑并显示新的周和年视图:
<a href="<%=Url.Action("Next", new { number=ViewData["WeekNumber"], year = ViewData["Year"] })%>">
>></a>
但我想在 jQuery 中执行此操作(或直接编辑)...
I am trying to call an action method by jQuery, and got help with passing parameters here: Calling action method in MVC with jQuery and parameters not working . However, even though the parameters are sent correctly now, in the action method I need to redirect (passing on those same parameters) to another action method. In the debugger I can see that the redirect is carried out, and the parameters are still there, but the View returned doesn't update accordingly...
Is there some problem with calling an action method that redirects by using jQuery?
Action method called by jQuery:
public ActionResult SelectDate(string date)
{
DateTime dateObject = DateTime.Parse(date);
MyCalendar calendar = new MyCalendar();
string number = calendar.GetWeekNumber(dateObject).ToString();
string year = dateObject.Year.ToString();
return RedirectToAction("Edit", new { number = number, year = year });
}
Action method redirected to:
public ActionResult Edit(string number, string year) //Why string here???
{
int n;
if (string.IsNullOrEmpty(number))
n = myCalendar.GetWeekNumber(DateTime.Today);
else
n = Int32.Parse(number);
int y;
if (string.IsNullOrEmpty(year))
y = DateTime.Today.Year;
else
y = Int32.Parse(year);
List<Customer> customers = _repository.Customers;
ViewData["Customers"] = new SelectList(customers, "CustomerId", "CustomerName");
ViewData["WeekNumber"] = n;
ViewData["Year"] = y;
return View();
}
Or is jQuery get() not the proper way to call an action method to load a new page? Note that doing the same thing with an ActionLink works fine, it's just that I need to call it by jQuery because I'm using the datepicker plugin and also a button as the way to call the action.
What am I doing wrong?
UPDATE:
Actually, the RedirectToAction doesn't seem to be the problem, I modified the code so that the jQuery now calls the final action method directly (by doing the job of the first action method directly in the jQuery, i.e. convert date to week and year). But I still don't get the new page with the new week and year.
Here's the new jQuery:
function selectWeek() {
$('#selectWeekButton').click(function (event) {
var date = $('#selectWeekId').val();
var selectedDate = new Date(date);
var year = selectedDate.getFullYear();
var week = selectedDate.getWeekOfYear();
var url = '<%: Url.Action("Edit") %>';
$.get(url, { number: week, year: year }, function (data) {
// alert('Test');
});
});
}
Again, is it using the get that is incorrect? I do not wish to load content in an html tag, I just want to use jQuery to do the exact same thing as an actionlink, i.e. call an action method to get the Edit View...
For instance, this actionlink works fine, calling an action method called Next that redirects to Edit and shows the new week and year View:
<a href="<%=Url.Action("Next", new { number=ViewData["WeekNumber"], year = ViewData["Year"] })%>">
>></a>
But I want to do that (or Edit directly) in jQuery...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jquery AJAX 自动遵循重定向,这意味着在成功回调中您将获得重定向到的
Edit
操作的结果。在此回调中,您将获得控制器操作返回的部分 HTML。因此,如果你想更新 DOM 的某些部分,你需要明确地指示它:或者简单地使用
.load()
方法:jquery AJAX automatically follows redirect meaning that in the success callback you will get the result of the
Edit
action you have redirected to. In this callback you will get the partial HTML returned by the controller action. So if you want to update some part of the DOM you need to explicitly instruct it so:or simply use the
.load()
method: