2013-04-02 53 views
3

我有一個控制器和視圖工作,調用Web服務,並返回結果。目前,它是我的默認控制器,名爲Home,它使用Index視圖頁面。MVC4和路由

它的工作。我可以發佈數據,然後在刷新的屏幕上放一些東西。它重新加載相同的視圖。

現在,一旦我提交了,我得到了一個很好的回覆,我想加載一個不同的控制器/視圖。

我的路線是這樣的現在:

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" }); 

我創建了一個控制所謂的位置,它只是有這樣:

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

在我家的控制器,我做的邏輯,然後嘗試加載新頁面。

 [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"); 

     } 

但是當我運行它,提交,步,它擊中RedirectToAction - 但...主屏幕只是刷新。我從來沒有看到新的位置視圖。我在這裏做錯了什麼?我have't掌握的路線還沒有......我需要一個新的對象傳遞給Location.Index屏幕顯示...

回答

1

你的路由映射是不正確,檢查了這一點: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" }); 
+0

Rob,我試過了,但默認的開始屏幕重新加載。什麼是starnge是位置控制器中的「返回View()」運行 - 但返回錯誤的視圖... – Craig

+0

啊,等等...我刪除了其他的路線,這個工程。 THhanks。 – Craig

-1

嘗試使用以下的製程代碼

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", lang="English" }); 
+0

這導致在路由表中沒有路由的SUP匹配合理價值。 – Craig

-1

我不這麼認爲,你需要做任何改動。至於你的情況,你想用它的尊以加載不同的控制器,你需要下面的改變只

與此代碼return Redirect("http://www.yoursite.com/Location/Index");

你的變化是像重定向從一個網頁到另一個因此將這段代碼return RedirectToAction("Index", "Location");

你需要把你的完整路徑這裏

請儘量&迴應,如果有任何問題