簡體   English   中英

MVC4和路由

[英]MVC4 and Routing

我有一個控制器和視圖工作,調用Web服務,並返回一個結果。 目前,它是我的默認控制器,稱為Home,它使用索引視圖頁面。

它正在發揮作用。 我可以發布數據,然后在刷新的屏幕上放一些東西。 它重新加載相同的視圖。

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

我的路線現在看起來像這樣:

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

我創建了一個名為Location的受控制,它只是這樣:

 //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 - 但是......主屏幕只是刷新。 我從未看到新的位置視圖。 我在這做錯了什么? 我還沒有抓住路線......我需要將一個新對象傳遞給Location.Index屏幕以顯示...

您的路線映射不正確,請查看: 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" });

我不這么認為你需要做任何改變。 在您的情況下,您希望加載不同的控制器及其尊重視圖,您只需要在下面進行更改

替換此代碼return RedirectToAction("Index", "Location");

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

您的更改就像從一個頁面重定向到另一個頁面,因此您需要將完整路徑放在此處

如果有任何問題,請嘗試回復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM