2012-03-31 33 views
1

全部,将模型传递给另一个视图

我是MVC的新手,因此在学习新项目时学习了它。我有一些我想实现的简单功能,但不知道我是否以正确的方式进行操作 - 目前的方法会导致运行时错误 - 以下是对问题的描述。

我正在尝试创建一些密码重置功能。要重置密码,我要让用户在一个视图中输入他们的用户名和电子邮件。然后在第二个视图中,我将显示他们的密码重置问题,并让他们输入密码重置答案。第二个视图也会显示他们的用户名,所以我需要将输入的用户名从视图1传递到视图2。我有以下两种模式至今:

public class ResetPasswordModelStepOne 
{ 
    [Required] 
    [Display(Name = "Username")] 
    public string Username { get; set; } 

    [Required] 
    [Display(Name = "Email")] 
    [DataType(DataType.EmailAddress)] 
    public string Email { get; set; } 
} 

public class ResetPasswordModelStepTwo 
{ 
    public ResetPasswordModelStepOne StepOneModel { get; set; } 

    [Display(Name = "Question")] 
    public string ResetQuestion { get; set; } 

    [Required] 
    [Display(Name = "Answer")] 
    public string ResetAnswer { get; set; } 
} 

注意:第二个模型还具有存储步骤一个模型的属性,这是为了让在第二视图,我可以访问并显示用户的用户名像“Hi {用户名}”这样的消息,要重置您的密码,请回答您的密码重置问题。“我为上述模型创建了强类型视图,并执行以下操作。

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

    [HttpPost] 
    public ActionResult PasswordResetStepOne(ResetPasswordModelStepOne stepOneModel) 
    { 
     //Imagine i'm validating that the user exists here and then retrieving 
     //their secret question from a repository 
     var userSecretQuestion = "What is your favourite color?"; 

     return PasswordResetStepTwo(new ResetPasswordModelStepTwo { StepOneModel = stepOneModel, ResetQuestion = userSecretQuestion }); 
    } 

    public ActionResult PasswordResetStepTwo(ResetPasswordModelStepTwo stepTwoModel) 
    { 
     return View(stepTwoModel); 
    } 

我用这种方法遇到的问题是,当用户输入他们的第一步观点用户名和电子邮件,我则称之为“PasswordResetStepTwo”行动,其返回ResetPasswordModelStepTwo强类型的视图 - 这导致以下运行时错误:

The model item passed into the dictionary is of type 'MvcCrossPageModel.Models.ResetPasswordModelStepTwo', but this dictionary requires a model item of type 'MvcCrossPageModel.Models.ResetPasswordModelStepOne'.

有人可以解释我在做什么错吗?用一种模式在一种视图中实现这一点有更好的方法吗?我是否通过创建“第一步”和“第二步”模型来正确执行此操作?理想情况下,我想有一个单一视图,用户输入自己的用户名和电子邮件 - 那么同样的看法返回提示他们输入密码提示问题/

+0

什么视图模型的PasswordResetSteptTwo看法? – MikeSW 2012-03-31 09:25:06

+0

第二步视图的模型是ResetPasswordModelStepTwo。我收到了另一个人在这里提出的意见,建议在PasswordResetStepOne POST中使用RedirectToAction,并使用TempData将用户名和电子邮件从第一步传递到第二步。我已经这样做了,它工作的很好 - 尽管我仍然不确定这个简单的功能是否有两个独立的视图是合理的,但是在MVC中只有一个视图无法实现? – DotNetDeveloper 2012-03-31 09:44:47

回答

2
every strongly typed views has one model. But you have two ways. 
Razor syntax 
1.Use this view. 
    @model dynamic 
    <div> 
    any html 
    @(using Html.BeginForm()){ 
    @Html.EditorForModel() 
    <button type="submit">save</button> 
    } 
    </div> 

2. Use two views 

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

    [HttpPost] 
    public ActionResult PasswordResetStepOne(ResetPasswordModelStepOne stepOneModel) 
    { 
     //Imagine i'm validating that the user exists here and then retrieving 
     //their secret question from a repository 
     var userSecretQuestion = "What is your favourite color?"; 

     return PasswordResetStepTwo(new ResetPasswordModelStepTwo { StepOneModel = stepOneModel, ResetQuestion = userSecretQuestion }); 
    } 

    public ActionResult PasswordResetStepTwo(ResetPasswordModelStepTwo stepTwoModel) 
    { 
     return View("NextViewName",stepTwoModel); 
    } 
+0

啊,我不知道你可以有@model的动态 - 我会尝试这种方式,并返回给你user1304836 - 不幸的是我现在不在家里,所以当我回家时会试一试。这看起来很有希望,谢谢。 – DotNetDeveloper 2012-03-31 09:47:18

+0

谢谢 - 我去了动态模型选项 – DotNetDeveloper 2012-04-01 07:49:07

+0

我很乐意帮助你! – Lamer 2012-04-01 07:59:19

相关问题