2017-10-02 5 views
0

私の最初のコントローラ:のasp.net mvc5にルーティングするように値を渡す

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

[HttpPost] 
public ActionResult Create(personalInfoModel personalInfo) 
{ 
    if (ModelState.IsValid) 
    { 
     TempData["key"] = personalInfo.CNIC; 

     PersonalInfoViewModel pivm = new PersonalInfoViewModel(); 
     pivm.AddNewRecord(personalInfo); 

     return RedirectToAction("Create", "Experience", new { key = personalInfo.CNIC}); 
    } 

    return View(); 
} 

そして、私の第二のコントローラのコードは次のとおりです。

public ActionResult Create(string key) 
{ 
    if (filled == true) 
    { 
     TempData["alertMessage"] = "<script>alert('Fill it first!')</script>"; 
    } 
    filled = true; 
    return View(key); 
} 

[HttpPost] 
public ActionResult Create(experiencesModel experiences, string key) 
{ 
    filled = false; 
    ExperiencesViewModel evm = new ExperiencesViewModel(); 
    evm.AddNewRecord(experiences, key); 
    return View(); 

} 

私はここで第二のコントローラに第一のコントローラからのキーを渡したいです私はエラーに直面しています:

The view '42201-09007860-1' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Experience/42201-09007860-1.aspx ~/Views/Experience/42201-09007860-1.ascx ~/Views/Shared/42201-09007860-1.aspx ~/Views/Shared/42201-09007860-1.ascx ~/Views/Experience/42201-09007860-1.cshtml ~/Views/Experience/42201-09007860-1.vbhtml ~/Views/Shared/42201-09007860-1.cshtml ~/Views/Shared/42201-09007860-1.vbhtml

どうすればこの問題を解決できますか?コントローラからコントローラへの値渡しについての明確化が必要です。

+0

RouteValueDictionaryの代わりにオブジェクトを受け取りRedirectToActionに2番目のオーバーロードがあります。あなたの新しい{}がオブジェクトとして誤って認識されているのでしょうか?実際のRouteValueDictionaryを作成してみてください。 https://msdn.microsoft.com/de-de/library/dd460311(v=vs.118).aspx – SlapY

答えて

1

Viewの最初のパラメータは、レンダリングするビューの名前です。規約に基づいてデフォルトビューを使用する場合は、nullを渡す必要があります。 2番目のパラメータは、モデルを渡すことができます。keyです。

return View(null, key); 

それとも

return View("Create", key); 
1

私はあなたが完全にreturn View()コードを理解していないと思います。

return View() returns the view corresponding to the action method and return View("ViewName") returns the view name specified in the current view folder.

この行で:

return View(key); 

あなたはkeyパラメータで提供された名前を持つビューを返すようにしようとしている、機能しません。ここView()View("ViewName")の違いですもちろん。あなたがCreateページを表示したい場合は、単に次の行のように変更します。

return View("Create", key); 
関連する問題