简体   繁体   中英

MVC4 and Routing

I have a single controller and view working that calls a web service, and returns a result. At the moment, it's my default controller, called Home, and it uses the Index view page.

It's working. I can post data and then put something on the refreshed screen. It reloads the same view.

Now, once I submit, and I get a good reply, I want to load a different controller/view.

My routes look like this right now:

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Home",
        "{lang}",
        new { controller = "Home", action = "Index", lang="English" });

    routes.MapRoute(
        "Location",
        "{lang}",
        new { controller = "Location", action = "Index", lang = "English" });

I created a controlled called Location, and it just has this:

 //LocationController 
 public class LocationController : Controller
 {
    public ActionResult Index()
    {
       return View();
    }
 }

In my home controller, I am doing the logic, and then attempting to load the new page.

        [HttpPost]
        public ActionResult Index(HomeModel model)
        {
            var proxy = new Proxy();
            var r = proxy.GetLocationByAddress(model.SearchString, o.ToString());
            if(r==null)
            {
                ViewBag.Error = "Error during search";
                return View(model);
            }
            ViewBag.Error = string.Format("Found {0} at {1}, {2}", r.StreetName, r.Latitude, r.Longitude);
            return RedirectToAction("Index", "Location");

        }

But when I run it, submit it, step through, it hits the RedirectToAction - but ... the Home screen simply refreshes. I never see the new Location view. What am I doing wrong here? I have't grasped Routes yet... I need to pass a new object to the Location.Index screen to display...

Your route mapping is incorrect, check this out: http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs

routes.MapRoute(
    "Location",
    "Location/{lang}",
    new { controller = "Location", action = "Index", lang = "English" });

I don't think so you need to make any changes. As in your case you want to load different controller with its respecting view you need below change only

replace this code return RedirectToAction("Index", "Location");

with this code return Redirect("http://www.yoursite.com/Location/Index");

Your change is like redirection from one page to another therefore you need to put your complete path here

please try & reply if any problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM