2016-01-20 70 views
0

新來的MVC所以學習(在一個失意的一種方式尋找簡單的東西,以前需要在web表單5分鐘已經變得過於複雜)MVC鏈接到的意見

所以我成立了VS2015的解決方案它附帶先決條件帳戶部分。

我還設置了數據庫中的表和建立不同於實體(生產一些CRUD

這樣我就可以登錄並(在AccountControllerreturn View("AccountHome")

讓我AccountHome視圖從我AccountHome頁面,我可以用一個單獨的控制器
@Html.ActionLink("Add Foods", "Create", "fad_userFoods", new { id = ViewBag.userID }, new { @class = "btn btn-default" })

我在這裏做一些形式錄入和數據成功添加到數據庫導航到另一個看法,但這裏是我的問題,李ES。數據錄入後,我想回去AccountHome視圖

所以在我的fad_userFoodsControler我有這樣的代碼
return RedirectToAction("AccountHome","Account")
- 說資源不能被發現!

在我AccountController,我有以下代碼:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult AccountHome() 
{ 
    return View(); 
} 

在我AccountViewModels我有這樣的代碼:

public class AccountHomeViewModel 
{ 
    public string Email { get; set; } 
} 

最後,在我的AccountHome.cshtml頁我在上面這個聲明(@model FiveADayMVC2.Models.AccountHomeViewModel

我哪裏錯了?看了很多帖子,他們都這樣做。我知道該視圖存在,因爲我可以從登錄中獲得該視圖。然而,如果我手動輸入網址(即我的網址後跟/Account/AccountHome),它不會被發現?

這將是我的缺乏理解,但在哪裏?

預先感謝任何指針

+0

你可以通過縮進代碼來清理你的問題嗎? –

回答

1

嘗試使用RedirectToAction這將導航用戶在一個給定的控制器的特定動作的圖。

if(actionSuccess) 
{ 
    return RedirectToAction("Index", "Home"); 
} 

UPDATE:

確保你有一個控制器HTTPGET

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult AccountHome() 
{ 
    return View(); 
} 

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

既然你加入[HttpPost]控制器將是一個職位之後執行的一個關於「作出AccountHome「

+0

嗨John,我確實使用它(RedirectToAction(「AccountHome」,「Account」)) AccountHome是我設置的一個新視圖。 如果我使用由默認解決方案設置的視圖(例如RedirectToAction(「ForgotPassword」,「Account」)) 只是不適用於我設置的AccountHome視圖。 :-( – stumcc

+0

我可以確認該視圖存在。我知道這是因爲(不僅在服務器上直觀地檢查它),我使用以下代碼成功導航到頁面: return View(「AccountHome」) – stumcc

+1

我更新了我的答案。希望它有幫助 –