locked
After post action the redirect dosen't work RRS feed

  • Question

  • User1185448985 posted

    I'm working with asp.net core MVC project and i faced this issue

    Where after the post action is completed the redirect to Index doesn't work(highlighted bellow)

    what could be the problem ?

    public async Task<IActionResult> Index()
            {
                ...........................
    
    
                var stories = await _db.ChildStory
                                       .Include(c => c.Child)
                                       .Include(s => s.Story)
                                       .ToListAsync();
    
                return View(stories);
            }
    
    
    
    //GET
            public async Task<IActionResult> Create()
            {
                    ................
                CreateStoryViewModel modelVM = new CreateStoryViewModel()
                {
                    ChildrenList = await _db.Childs.Where(c => c.ParentOf.InstitutionId == currentSpecialist.InstitutionId).ToListAsync(),
    
                    Child = new Models.Childs(), 
                    Story = new Models.Story(),
                    Image = new Models.Image(),
                    Audio = new Models.Audio(),
                };
    
                return View(modelVM);
            }
    
            //POST
            [HttpPost]
            //[ValidateAntiForgeryToken] it prevents recieving json
            public async Task<IActionResult> Create(CreateStoryViewModel model, string sentCount, List<string> sentences)
            {
                ................
    
                if (ModelState.IsValid)
                {
    
                    ................
    
                    return RedirectToAction(nameof(Index));
    
                }
    
    
                CreateStoryViewModel modelVM = new CreateStoryViewModel()
                {
                    ChildrenList = await _db.Childs.Where(c => c.ParentOf.InstitutionId == currentUser.InstitutionId).ToListAsync(),
                    Child = model.Child, //why?
                    Story = model.Story,
                    Sentence = model.Sentence,
                    Image = model.Image,
                    Audio = model.Audio,
                };
                return View(modelVM);
            }

    Tuesday, August 11, 2020 11:10 PM

Answers

  • User-474980206 posted

    Also if this is called via Ajax (comment says it returns json), the redirect response is in the Ajax response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 2:41 PM
  • User475983607 posted

    Amani Al

    Yes it is called by Ajax

    Please elaborate more, what do you mean by "the redirect response is in the Ajax response"

    When an HTTP request is made from code, like JavaScript/jQuery, the response is returned to the code.   It is up to you to write code that does something with the response. 

    Write a standard form post if you expect the browser to refresh the page.   Use AJAX if you plan to update a section of the page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 11:09 PM
  • User711641945 posted

    Hi Amini AI,

    If you called by ajax,you could refer to the following link about how to redirect after ajax success:

    https://stackoverflow.com/questions/63352668/redirect-page-with-jquery-in-asp-net-core/63352919#63352919

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 1:10 AM

All replies

  • User-821857111 posted

    Most likely, the modelstate is not valid. You need to use the debugger to find out for sure: https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Wednesday, August 12, 2020 5:38 AM
  • User-474980206 posted

    Also if this is called via Ajax (comment says it returns json), the redirect response is in the Ajax response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 2:41 PM
  • User1185448985 posted

    Yes it is called by Ajax

    Please elaborate more, what do you mean by "the redirect response is in the Ajax response"

    thank you

    Wednesday, August 12, 2020 10:48 PM
  • User475983607 posted

    Amani Al

    Yes it is called by Ajax

    Please elaborate more, what do you mean by "the redirect response is in the Ajax response"

    When an HTTP request is made from code, like JavaScript/jQuery, the response is returned to the code.   It is up to you to write code that does something with the response. 

    Write a standard form post if you expect the browser to refresh the page.   Use AJAX if you plan to update a section of the page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 11:09 PM
  • User711641945 posted

    Hi Amini AI,

    If you called by ajax,you could refer to the following link about how to redirect after ajax success:

    https://stackoverflow.com/questions/63352668/redirect-page-with-jquery-in-asp-net-core/63352919#63352919

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 1:10 AM
  • User1185448985 posted

    i added the action name in the success as follow:

    $.ajax({
                    type: "POST",
                    url: "/Story/Create",
                    contentType: false,
                    processData: false,
                    data: data,
                    success: function (message) {
                        window.location.href = "Index";
                    },
                    error: function () {
                        alert("Error");
                    }
                });

    thank you all

    Friday, August 14, 2020 8:24 PM