RedirectToAction not working when called by jQuery?

空扰寡人 提交于 2019-12-11 02:54:33

问题


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

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


回答1:


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

or simply use the .load() method:

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


来源:https://stackoverflow.com/questions/4545901/redirecttoaction-not-working-when-called-by-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!