如何在Mvc中保持页面数据? [英] How Can I Persist Data Across Pages In Mvc?

查看:88
本文介绍了如何在Mvc中保持页面数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景是用户将选中多个复选框,我需要存储所有ID(对于选择复选框)。现在用户将点击一个特定的按钮,在那里他将被重定向到另一个页面。现在我需要使用ids。这个ID实际匹配文档ID并使用这些ID我需要实现一个寻呼机,显示所有那些用户选择ID的文件。





它很复杂,但任何有用的信息都会受到赞赏。我需要一种最好的方法来实现这一目标。我试图避免使用部分和cookie来执行相同操作。

解决方案

您可以使用具有操作的表单带有这些复选框的属性。这样,当您提交表单时,此表单的所有数据都将发送到下一页,您可以在其中操作数据并向用户显示相关内容。例如,以下表格,



 <  表单   方法  =  post    action   =  second_page.aspx >  
<! - 此处的表单元素... - >
< / form >





现在,当用户提交它时,它将被重定向(自动)到第二页,并且数据会出现在那里。您可以访问数据,



  var  val =请求[   input-name]; 





还有其他方法可以做同样的事情。其中包括,如果您想要执行手动重定向或其他类似的操作,会话变量可以很好地替代它。其次,如果您想在客户端存储这些值,那么您也可以使用localStorage或sessionStorage(在MDN上读取存储API [ ^ ])。如果您不想使用表单等,它们也是表单值的重要替代品。请阅读我的这篇文章,其中涵盖了类似的主题如何使用ASP.NET在不同网页之间共享数据 [ ^ ]。


是的,你可以。使用ViewData,ViewBag或TempData。

浏览什么是ViewData,ViewBag和TempData ? - 用于在当前和后续请求之间传递数据的MVC选项 [ ^ ]。

My scenario is user will select multiple check box and i need to store all the ids(for the select checkbox).Now user will hit a particular button where he will be redirected to another page.Now in that page i need to use the ids .This ids actually matches to a document id and using these ids i need to implement a pager which display all those documents having ids selected by user.


It's bite complicated but any useful information will be appreciated.I need a best way to achieve this. I am trying to avoid sections and cookies for doing the same.

解决方案

You can use a form having an action attribute with these check boxes. That way, when you will submit the form, all of the data of this form would be sent to the next page where you can manipulate the data and show related content to the user. For example the following form,

<form method="post" action="second_page.aspx">
   <!-- Form elements here... -->
</form>



Now when user will submit it, it would be redirected (automatically) to the second page and would have the data present there. You can access the data as,

var val = Request["input-name"];



There are others methods of doing the same thing. Which includes, Session variables can be a good replacement for this if you want to perform a manual redirect or something like that. Secondly, if you want to store these values on the client-side, then you can use localStorage or sessionStorage too (Read storage API on MDN[^]). They also provide a vital replacement for form values, if you don't want to use a form and so on. Please read this article of mine, which covers the similar topic "How to share data among different web pages using ASP.NET[^]".


Yes you can. Use either of ViewData, ViewBag or TempData.
Go through What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request[^].


这篇关于如何在Mvc中保持页面数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆