简体   繁体   English

在mvc4中维护视图之间的数据

[英]maintaining data between views in mvc4

I would like to know how I can flow data between multiple actions in MVC4. 我想知道如何在MVC4中的多个动作之间传递数据。 For example, user is landing at one page (non-authenticated), fills in some data then goes to 2nd view, fills in another set of data, and then on submit, the code checks if the user is authenticated then proceeds further, otherwise go to login / register view, authenticate and redirect to 3rd step. 例如,用户登陆一个页面(未经过身份验证),填写一些数据然后转到第二个视图,填写另一组数据,然后在提交时,代码检查用户是否经过身份验证然后继续进行,否则转到登录/注册视图,进行身份验证并重定向到第3步。 Now, I want to know how I can keep the data for that user while they are authenticating, should I put the data in session object and once the user is done with authentication retrieve the data? 现在,我想知道如何在对用户进行身份验证时保留数据,我应该将数据放在会话对象中,一旦用户完成身份验证检索数据? I'm not sure how to do it in MVC the right way, since its a bit different compared to web forms. 我不确定如何以正确的方式在MVC中实现它,因为它与Web表单相比有点不同。

Thanks in advance, Laziale 提前谢谢,Laziale

In these scenarios is not different from WebForm. 在这些场景中与WebForm没有什么不同。 You can use the Session even if usually is preferable not to store state in the server, you can also use a cookie (if the data is small). 您可以使用Session,即使通常最好不要在服务器中存储状态,也可以使用cookie(如果数据很小)。

What you could do is to create a TempData key in your initial controller and when the value is returned, its value would be what the user has input. 你可以做的是在初始控制器中创建一个TempData键,当返回值时,它的值将是用户输入的值。

In your controller action: 在您的控制器操作中:

 [HttpPost]
 public ActionResult LandingPage(LandingPageViewModel model)
 {
    TempData["Model"] = model;
    return RedirectToAction("OtherDataPage");
 }

So on your landing page, when the user sends input, you store it in TempData and then redirect the user to the other page to fill information. 因此,在您的目标网页上,当用户发送输入时,您将其存储在TempData中,然后将用户重定向到另一页以填充信息。

In the other action, you can use TempData to set object values from the user's previous input. 在另一个操作中,您可以使用TempData从用户的先前输入设置对象值。

 public ActionResult OtherDataPage()
 {
    LandingPageViewModel model = new LandingPageViewModel();
    model = TempData["Model"];
    return View();
 }

Something like that should persist the user input 这样的事情应该坚持用户输入

您可以使用“TempData”或“Viewbag”获取更多信息,您可以看到此链接什么是ViewData,ViewBag和TempData?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM