Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 - RedirectToAction Not Redirecting

Hi I have a problem with my RedirectToAction not redirecting. My code successfully hits a breakpoint placed on the Redirect so I can confirm it is being called. Nothing seems to happen however.

I have tried looking at the network traffic in Chrome and there doesn't seem to be anything obvious. I must be missing something simple!

    //
    // POST: /Blog/CreateBlog
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateBlog(BlogViewModel model)
    {
        var userId = User.Identity.GetUserId();
        model.UserId = userId;

        if (ModelState.IsValid && model.UserId != null)
        {
            Mapper.CreateMap<BlogViewModel, Blog>();
            if (_blogProcess.CreateBlog(Mapper.Map<BlogViewModel, Blog>(model)))
            {
                RedirectToAction("Index", "Blog");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
like image 462
JscNZ Avatar asked Dec 10 '13 10:12

JscNZ


People also ask

What is redirecttoaction method in MVC?

RedirectToAction Result in MVC The RedirectToAction Result is returning the result to a specified controller and action method. Controller name is optional in RedirectToAction method. If not mentioned, Controller name redirects to a mentioned action method in the current Controller.

How do I redirect to a specific action in HTML?

The RedirectToAction() Method. This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action.

What is redirecttorouteresult in ASP NET MVC?

It is rendered to the page by URL. If we give the wrong URL, it will show 404-page errors. The RedirectToRouteResult is used whenever we need to go from one action method to another action method within the same or different controller in ASP.NET MVC Application.

How to create redirect action result in ASP NET Core MVC?

Redirect Action Result in ASP.NET Core MVC. Step 1. Open Visual Studio 2019 and select the ASP.NET Core Web Application template and click Next. Step 2. Name the project FileResultActionsCoreMvc_Demo and click Create. Step 3. Select Web Application (Model-View-Controller), and then select Create. ...


2 Answers

try

return RedirectToAction("Index", "Blog");
like image 142
Nalaka526 Avatar answered Oct 21 '22 10:10

Nalaka526


In addtion to Nalaka526's answer: if we look into the documentation for RedirectToAction we can see that it's an instance method of the Controller class which has RedirectToRouteResult as return type, which derives from ActionResult which indicates that we have to return it, just like we return View and PartialView for example.

like image 32
Henk Mollema Avatar answered Oct 21 '22 08:10

Henk Mollema