2017-04-21 145 views
1

我需要將數據從一個控制器動作傳遞給另一個。 我不想用將數據從一個控制器動作傳遞到另一個控制器動作在ASP.Net MVC 5

a。 Session

b。 TempData的

邏輯

下面代碼是關於forget password。用戶鍵入他的電子郵件密碼重置,然後重定向到另一個視圖,他可以通過電子郵件接收令牌。現在我想從第一個視圖發送emailID作爲隱藏字段(或WHATEVER),這樣我可以發送令牌以及電子郵件ID(隱藏),以便在發佈第二個視圖時進行驗證。 下面好吧是我曾嘗試

FirstMV.cs

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

SecondMV.cs

public class SecondMV 
{ 
    public string Token { get; set; } 
    public string Email { get; set; } 
} 

FirstView.cshtml

@model FirstMV 
@Html.LabelFor(m => m.Email) 
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
@Html.ValidationMessageFor(m => m.Email) 

SecondView.cshtml

@model SecondMV 
@Html.LabelFor(m => m.Token) 
@Html.TextBoxFor(m => m.Token, new { @class = "form-control" }) 
@Html.ValidationMessageFor(m => m.Token) 
// HERE I WANT TO PUT ONE HIDDEN FIELD MAY BE FOR EMAIL ID 

控制器:

[HttpGet] 
public ActionResult FirstView() 
{ 
    return View(); 
} 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult ForgotPassword(FirstMV viewModel) 
{ 
    // Some code 
    return RedirectToAction("SecondView", new SecondMV { Email = viewModel.Email }); 
} 

[HttpGet] 
public ActionResult SecondView(SecondMV viewModel) 
{ 
    return View(); 
} 
[HttpPost] 
public ActionResult SecondView(SecondMV viewModel) 
{ 
    // Some Code 
    return View(); 
} 

爲SecondView被定義了兩次,我得到錯誤。

問題1:是我實現上述功能的唯一方法是TempData and Session。我是初學者,所以請指導我。如果最佳做法是在我的方案中使用Tempdata and Session,請告訴我。我會遵循這一點。

問題2:我們可以幫助「傳遞匿名對象」到另一個控制器動作。

+0

以前從未真正看到過使用[HTTPGet]。嘗試使用ViewBags將數據從視圖傳遞到控制器。對於隱藏信息,請嘗試@ Html.HiddenFor(x => x.EmailId)嘗試返回RedirectToAction(「SecondView」,new {EmailId = EmailId,message =「此人沒有電子郵件。」});其中消息是視圖包的名稱 – Edward

+0

在MVC中,使用HttpGet和HttpPost將允許您擁有2個相同名稱的Action方法......一個用於訪問頁面,然後一個用於跳出頁面....看到我下面的例子。它可以讓你準確地發送OP所詢問的內容。通常情況下,我會建議遠離ViewBag,除非你可以幫助它......如果使用MVC,堅持使用強類型模型....非常強大。 – user1011627

+0

您的'SecondView()'GET方法需要'返回View(viewModel);'並在視圖中@@ Html.HiddenFor(m => m.Email)'。但GET方法的簽名需要爲'public ActionResult SecondView(字符串電子郵件)',並且您在方法 –

回答

0

您遇到的問題是您已在第二個操作集(在我的示例中爲ForgotPasswordCode)上創建了重複簽名。您需要將新模型類型發送到第二組獲取中,或者發送FirstMV,這正是我所展示的。試試這個,看看它是否適合你:

控制器操作:

[HttpGet] 
    public ActionResult ForgotPassword() 
    { 
     Models.FirstMV model = new Models.FirstMV(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult ForgotPassword(Models.FirstMV model) 
    { 
     // Do forgot password stuff and redirect 

     Models.SecondMV secondModel = new Models.SecondMV 
     { 
      Email = model.Email, 

     }; 

     return RedirectToAction("ForgotPasswordCode", "Home", secondModel); 
    } 

    [HttpGet] 
    public ActionResult ForgotPasswordCode(Models.FirstMV model) 
    { 
     Models.SecondMV secondModel = new Models.SecondMV 
     { 
      Email = model.Email 
     }; 

     return View(secondModel); 
    } 

    [HttpPost] 
    public ActionResult ForgotPasswordCode(Models.SecondMV model) 
    { 
     if (model.Token == "MyTokenValue") 
     { 
      return RedirectToAction("ForgotPasswordSuccess", "Home"); 
     } 
     else 
     { 
      return RedirectToAction("ForgotPasswordFailed", "Home"); 
     } 
    } 

    [HttpGet] 
    public ActionResult ForgotPasswordSuccess() 
    { 
     return View(); 
    } 

    [HttpGet] 
    public ActionResult ForgotPasswordFailed() 
    { 
     return View(); 
    } 

密碼忘FORM

@model WebApplication11.Models.FirstMV 

@using (Html.BeginForm("ForgotPassword", "Home", new { Email = Model.Email }, FormMethod.Post)) 
{ 

    @Html.LabelFor(m => m.Email) 
    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
    @Html.ValidationMessageFor(m => m.Email) 
    <button type="submit">Submit</button> 

} 

密碼忘了代碼的形式

@model WebApplication11.Models.SecondMV 


@using (Html.BeginForm("ForgotPasswordCode", "Home", new { Email = Model.Email, Token = Model.Token }, FormMethod.Post)) 
{ 

    @Html.LabelFor(m => m.Token) 
    @Html.TextBoxFor(m => m.Token, new { @class = "form-control" }) 
    @Html.HiddenFor(x => x.Email) 
    @Html.ValidationMessageFor(m => m.Token) 
    <button type="submit">Submit</button> 
} 
+0

中初始化新的'SecondMV''返回RedirectToAction(「ForgotPasswordCode」,「Home」,secondModel);'您正在返回在httppost方法中'returnredirect'中的SecondModel實例。但是在用於SecondView控制器操作的httpget中,您將其存儲在參數'Models.FirstMV模型中' – Unbreakable

+0

'Model.FirstMV模型'可如何存儲和接受'secondModel'類型的返回值? – Unbreakable

+0

這就是我的意思說:「你要麼需要發送一個新的模型類型到第二組的Get中,或者發送FirstMV,這正是我所展示的。」最好的做法是創建一個新的模型發送到HttpGet的ForgotPasswordCode,但我剛剛重新使用FirstMV,因爲它已經有了電子郵件,這就是我所關心的例子。如果你的模型名稱具有更好的語義含義,那麼這可能不會造成混淆。這恰好是MVC很好地處理了我的強制問題,但我同意這樣寫就顯得有點奇怪。 HTH ... – user1011627

相關問題