Asp.net jQuery调用RedirectToAction时是否不工作?
我正试图通过jQuery调用操作方法,并在此处获得了有关传递参数的帮助:。但是,即使参数现在已正确发送,在action方法中,我需要重定向(传递这些相同的参数)到另一个action方法。在调试器中,我可以看到重定向已经执行,参数仍然存在,但是返回的视图没有相应地更新 使用jQuery调用重定向的操作方法是否有问题 jQuery调用的操作方法:Asp.net jQuery调用RedirectToAction时是否不工作?,asp.net,jquery,asp.net-mvc-2,redirecttoaction,Asp.net,Jquery,Asp.net Mvc 2,Redirecttoaction,我正试图通过jQuery调用操作方法,并在此处获得了有关传递参数的帮助:。但是,即使参数现在已正确发送,在action方法中,我需要重定向(传递这些相同的参数)到另一个action方法。在调试器中,我可以看到重定向已经执行,参数仍然存在,但是返回的视图没有相应地更新 使用jQuery调用重定向的操作方法是否有问题 jQuery调用的操作方法: public ActionResult SelectDate(string date) { DateTime dateOb
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();
}
public ActionResult Edit(字符串编号,字符串年份)//为什么在这里使用字符串???
{
int n;
if(string.IsNullOrEmpty(number))
n=myCalendar.GetWeekNumber(DateTime.Today);
其他的
n=Int32.Parse(数字);
int-y;
if(string.IsNullOrEmpty(年))
y=DateTime.Today.Year;
其他的
y=Int32.Parse(年);
列出客户=_repository.customers;
ViewData[“Customers”]=新的选择列表(Customers、“CustomerId”、“CustomerName”);
ViewData[“周数”]=n;
ViewData[“年”]=y;
返回视图();
}
或者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');
});
});
}
函数selectWeek(){
$('#选择WeekButton')。单击(函数(事件){
var date=$('#selectWeekId').val();
var selectedDate=新日期(日期);
var year=selectedDate.getFullYear();
var week=selectedDate.getWeekOfYear();
var url='';
$.get(url,{number:week,year:year},函数(数据){
//警报(“测试”);
});
});
}
再说一次,使用get是不正确的吗?我不希望在html标记中加载内容,我只想使用jQuery做与actionlink完全相同的事情,即调用action方法来获取编辑视图
例如,此actionlink工作正常,调用名为Next的操作方法,该方法重定向到编辑并显示新的周和年视图:
<a href="<%=Url.Action("Next", new { number=ViewData["WeekNumber"], year = ViewData["Year"] })%>">
>></a>
但是我想在jQuery中这样做(或者直接编辑…jQuery AJAX自动遵循重定向,这意味着在成功回调中,您将获得重定向到的
编辑操作的结果。在此回调中,您将获得控制器操作返回的部分HTML。因此,如果要更新DOM的某些部分,需要明确指示它:
$.ajax({
url: '<%= Url.Action("SelectDate") %>',
data: { date: '123' },
success: function(result) {
$('#someresultDiv').html(result);
}
});
@Anders Svensson,如果要重定向,请不要使用AJAX。您错过了AJAX的全部要点。当您只需要更新页面的一部分,而不需要重定向/重新加载整个页面时,AJAX就可以使用。如果您需要重定向,不要使用AJAX,请放置一个简单的锚定链接。@Darin:是的,我怀疑这有问题,我知道,但是我应该如何做与actionlink相同的事情,同时传递所需的参数(并从datepicker jQuery插件设置的输入字段中获取)?是否还有其他jQuery调用与actionlink相同的action方法,而不是get()或ajax?@Anders Svensson,如果您不需要,请不要使用ajax:使用简单的actionlink。好的,但再次强调:如何使用actionlink实现以下两个功能:1。有按钮而不是链接的。2.传递所需参数-jQuery datepicker插件设置的字段中的日期?@Anders Svensson,实现什么?
$('#someresultDiv').load('<%= Url.Action("SelectDate") %>', { date: '123' });