- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
任何人都可以解释一下何时使用
我有一个要求,我需要在 Controller 一中设置一个值,该 Controller 将重定向到 Controller 二, Controller 二将渲染 View 。
我尝试使用 ViewBag,当我到达 Controller 二时,该值丢失了。
我可以知道何时使用以及优点或缺点吗?
谢谢
最佳答案
1)TempData
允许您存储重定向后仍保留的数据。在内部,它使用 Session 作为后备存储,在进行重定向后,数据将被自动驱逐。模式如下:
public ActionResult Foo()
{
// store something into the tempdata that will be available during a single redirect
TempData["foo"] = "bar";
// you should always redirect if you store something into TempData to
// a controller action that will consume this data
return RedirectToAction("bar");
}
public ActionResult Bar()
{
var foo = TempData["foo"];
...
}
2)ViewBag, ViewData
允许您将数据存储在将在相应 View 中使用的 Controller 操作中。这假设该操作返回一个 View 并且不重定向。仅在当前请求期间存在。
模式如下:
public ActionResult Foo()
{
ViewBag.Foo = "bar";
return View();
}
在 View 中:
@ViewBag.Foo
或使用 ViewData:
public ActionResult Foo()
{
ViewData["Foo"] = "bar";
return View();
}
在 View 中:
@ViewData["Foo"]
ViewBag
只是 ViewData
的动态包装器,仅存在于 ASP.NET MVC 3 中。
话虽如此,这两个构造都不应该被使用。您应该使用 View 模型和强类型 View 。所以正确的模式如下:
查看模型:
public class MyViewModel
{
public string Foo { get; set; }
}
行动:
public Action Foo()
{
var model = new MyViewModel { Foo = "bar" };
return View(model);
}
强类型 View :
@model MyViewModel
@Model.Foo
<小时/>
在简短的介绍之后,让我们回答您的问题:
My requirement is I want to set a value in a controller one, that controller will redirect to ControllerTwo and Controller2 will render the View.
public class OneController: Controller
{
public ActionResult Index()
{
TempData["foo"] = "bar";
return RedirectToAction("index", "two");
}
}
public class TwoController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
Foo = TempData["foo"] as string
};
return View(model);
}
}
和相应的 View (~/Views/Two/Index.cshtml
):
@model MyViewModel
@Html.DisplayFor(x => x.Foo)
<小时/>
使用 TempData 也有缺点:如果用户在目标页面上按 F5,数据将会丢失。
就我个人而言,我也不使用 TempData。这是因为它在内部使用 session ,而我在应用程序中禁用 session 。我更喜欢一种更 RESTful 的方式来实现这一目标。即:在执行重定向的第一个 Controller 操作中,将对象存储在数据存储中,并在重定向时使用生成的唯一 ID。然后在目标操作上使用此 id 来取回最初存储的对象:
public class OneController: Controller
{
public ActionResult Index()
{
var id = Repository.SaveData("foo");
return RedirectToAction("index", "two", new { id = id });
}
}
public class TwoController: Controller
{
public ActionResult Index(string id)
{
var model = new MyViewModel
{
Foo = Repository.GetData(id)
};
return View(model);
}
}
View 保持不变。
关于asp.net-mvc-3 - ViewBag、ViewData 和 TempData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7993263/
我一直在阅读 Professional ASP.NET MVC 1.0 书籍,并阅读其他有关在 Controller 中使用 ViewModel 而不是 ViewData 到 View 的资料。但是后
在我的 Controller 中我一直在设置 ViewData.Model = DBModel.Table.Take(1).Single(); 其中 DBModel 是由 Linq-to-SQL 的
只是想知道绑定(bind)到 MVC View 的 ViewData 和绑定(bind)到 @Html 的 ViewData 之间的实际区别是什么帮助对象? 我写了一个页面,他们似乎不是指同一件事。
我的 ASP.NET MVC 4 应用程序有一个带有国家/地区下拉菜单的联系表。对于该菜单,我使用 Html.DropDownListFor 辅助方法。但是,如果未选择国家/地区,我会遇到异常:“没有
我将我的 HTML 存储在数据库中,以便页面从数据库加载 HTML 数据。我在我的 ASPX 页面中使用 ViewData 来填充页面。 HTML 存储在 NVARCHAR(MAX) 列中,它非常适合
我正在通过 MVC3 处理我的第一个应用程序,但它仍然是一个新手: 我正在尝试在母版页上成功我的 ViewData[] 因为它包含将在每个页面上使用的消息,但是当我尝试访问它时,它说: CS0103:
问题仅用于教育目的。 我有一个 MVC 应用程序,它有 3 个 Web Journeys,它们在许多方面相互镜像 - 这使得引入处理公共(public)部分的基类变得很方便。 我还使用了很多被放入 v
我已经尝试检查使用 asp.net mvc 的最佳实践,并且很多人说我们永远不应该使用 ViewData。我读过这个post似乎是这样。 我能想到使用 ViewData 的一个原因是,如果您只想将一个
我网站的一部分需要导航下拉列表的全局数据,现在我在一个属性中执行以下操作: ViewData["projects"] = new[] { new ProjectNav { Id = 1, Na
我正在尝试通过 AJAX 将 String 发送到 Controller ,并在 Controller 将返回的 View 中使用它: Index.cshtml $(document).r
在 MVC 应用程序中,我想获取外部 .js 文件中的 ViewBag、ViewData、TempData 值。我不想在 view.cshtml 中编写脚本,所以我向其中添加了外部 js 文件。但我无
我正在尝试填充 HTML。带有 selectlist 的下拉列表,其中填充了来自数据库调用的字符串值(位置地址)和文本(位置描述)字段。我将选择列表作为 View 数据传递给我的 View 。下拉列表
我想在模式窗口上显示 ViewData 的内容,在我的例子中它包含字符串。为此,我在我看来使用了以下代码。 To learn more about ASP.NET MVC visit ht
我有一个指向 Controller 的链接,该 Controller 呈现包含此 javascript 的页面。 $(document).ready(function () { var
我用一堆 css 样式属性填充 View 数据。 ";"> 我希望能够做与上述类似的事情。即使用放入 View 数据的样式并将它们用于内联样式。 我该怎么做?谢谢 最佳答案 你的双引号太多了。试试这
我搜索过这个东西,但没有得出结论,如果我有 5-6 个下拉列表在我的页面中,我将它们与 ViewData 或 ViewBag 绑定(bind),而不是使用模型,因为我发现它有点简单。有没有性能相关的问
嗨,我是MVC的新手,但我对如何构建将ViewData返回给他们的MVC用户控件一无所知。我希望有人能发布有关如何解决此问题的分步解决方案。如果您可以使解决方案非常详细,那将对您大有帮助。 抱歉,我的
我正在 ASP.Net Controller 中创建一个 ViewData,如下所示: var elements = db.Requests.GroupBy(user => user.submitUs
我在 MVC 3 中看到了 ViewBag。它与 MVC 2 中的 ViewData 有什么不同? 最佳答案 它使用 C# 4.0 动态功能。它实现了与 viewdata 相同的目标,应该避免使用强类
当 Controller 渲染基于模型的 View 时,您可以使用索引器从 ViewData 集合中获取属性(即 ViewData["Property"])。但是,我有一个共享用户控件,我尝试使用以下
我是一名优秀的程序员,十分优秀!