Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Forms Creating a Basic Form Redirecting to an Action

ASP.NET MVC Forms code challenge Redirect() not working even though System.Web.Mvc using statement is included.

I'm unable to return Redirect("Index") in the Report action result method below. I am receiving the following error message: IssuesController.cs(88,24): error CS0103: The name `Redirect' does not exist in the current context

IssuesController.cs
using System.Web.Mvc;
using IssueReporter.Data;
using IssueReporter.Models;

namespace IssueReporter.Controllers
{
    public class IssuesController : Controller
    {
        private IssuesRepository _issuesRepository;

        public IssuesController()
        {
            _issuesRepository = new IssuesRepository();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Report()
        {
            var issue = new Issue();

            return View(issue);
        }

        [HttpPost]
        public ActionResult Report(Issue issue)
        {
            if (ModelState.IsValid)
            {
                _issuesRepository.AddIssue(issue);
                return Redirect("Index");
            }

            return View(issue);
        }
    }
}

1 Answer

Steven Parker
Steven Parker
230,230 Points

As the error message indicates, there is no "Redirect" method in System.Web.Mvc.

Perhaps you were thinking of "RedirectToAction".

Thanks! This worked for for me.

I was able to get the "Redirect" method to work in Visual Studio, so maybe it is a discrepancy between .NET Core and the .NET Framework? The specific class that the Redirect method exists in is System.Web.Mvc.Controller. And in the video just before the code challenge the instructor uses the Redirect method in his code.