简体   繁体   English

MVC4和路由

[英]MVC4 and Routing

I have a single controller and view working that calls a web service, and returns a result. 我有一个控制器和视图工作,调用Web服务,并返回一个结果。 At the moment, it's my default controller, called Home, and it uses the Index view page. 目前,它是我的默认控制器,称为Home,它使用索引视图页面。

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: 我创建了一个名为Location的受控制,它只是这样:

 //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. 但是当我运行它时,提交它,逐步执行,它会点击RedirectToAction - 但是......主屏幕只是刷新。 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... 我还没有抓住路线......我需要将一个新对象传递给Location.Index屏幕以显示...

Your route mapping is incorrect, check this out: http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs 您的路线映射不正确,请查看: 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"); 替换此代码return RedirectToAction("Index", "Location");

with this code return Redirect("http://www.yoursite.com/Location/Index"); 使用此代码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 如果有任何问题,请尝试回复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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