当jQuery的所谓RedirectToAction不工作? [英] RedirectToAction not working when called by jQuery?

查看:192
本文介绍了当jQuery的所谓RedirectToAction不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用的jQuery操作方法,并得到了这里传递参数的帮助:<一href=\"http://stackoverflow.com/questions/4542333/calling-action-method-in-mvc-with-jquery-and-parameters-not-working\">Calling MVC中的操作方法与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...

有一些问题,呼吁通过使用jQuery重定向的操作方法?

Is there some problem with calling an action method that redirects by using jQuery?

的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方法重定向到:

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

或者是jQuery的获得()不是正确的方法来调用一个动作方法加载了新的一页?需要注意的是与ActionLink的做同样的事情工作正常,但它只是我需要的,因为我使用的日期选择器插件,也是一个按钮来调用操作的方法的jQuery来调用它。

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.

我在做什么错了?

更新:

其实,RedirectToAction似乎并不成为问题,我修改了code,使jQuery的,现在直接调用最后的操作方法(通过直接在jQuery的,即在做的第一个动作方法的工作转换日期为周,年)。但我还是不会与新的一周和今年获得新的一页。

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.

下面是新的jQuery的:

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

再次是使用得到是不正确?我不希望在HTML标签加载内容,我只是想使用jQuery做同样的事,作为一个ActionLink的,即调用的操作方法来获取编辑视图...

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...

例如,这ActionLink的正常工作,呼吁称为下一步的操作方法重定向到编辑,并显示了新的一周,一年查看:

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"]   })%>">
            &gt;&gt;</a>

不过,我想这样做(或直接编辑)jQuery中...

But I want to do that (or Edit directly) in jQuery...

推荐答案

jQuery的AJAX自动跟随重定向意味着在成功回调,您将获得修改作用的结果你重定向到。在这个回调您将获得由控制器操作返回的HTML部分。所以,如果你想更新DOM的某些部分,你需要明确地指示它这样:

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:

$.ajax({
    url: '<%= Url.Action("SelectDate") %>',
    data: { date: '123' },
    success: function(result) {
        $('#someresultDiv').html(result);
    }
});

或者干脆使用 .load() 方法:

$('#someresultDiv').load('<%= Url.Action("SelectDate") %>', { date: '123' });

这篇关于当jQuery的所谓RedirectToAction不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆