【问题标题】:How to view ViewBag content while debugging in Visual Studio?在 Visual Studio 中调试时如何查看 ViewBag 内容?
【发布时间】:2015-11-16 12:46:42
【问题描述】:
一个非常基本的问题:如何在 Visual Studio 中调试 ASP.NET MVC 控制器时轻松查看 ViewBag 的内容?
作为一种解决方法,我使用临时变量:
string tmp = ViewBag.MyData;
所以,问题是直接查看ViewBag.MyData 很困难,tmp 很容易。
【问题讨论】:
标签:
c#
asp.net-mvc
visual-studio
razor
【解决方案1】:
调试时查看ViewBag的内容:
- 在断点处...
- 打开(调试菜单/QuickWatch)并输入 ViewBag。
- 点击左侧的箭头选择 ViewBag,然后选择 Dynamic。
- 将显示 ViewBag 的内容。
【解决方案2】:
您可以轻松地将ViewBag 添加到Watcher。然后展开Dynamic View 以查看所有属性。
【解决方案3】:
我建议使用“View Models”,例如:
视图模型
public class HomeViewModel {
public string Text { get; set;}
}
控制器
public ActionResult Index() {
var viewModel = new HomeViewModel {
Text = "My Text"
};
return View(viewModel);
}
查看
@model MyApplication.ViewModels.HomeViewModel
This is my text: @Model.Text
【解决方案4】:
我可能在这里遗漏了要点 - 但是您能否不简单地在要查看的 ViewBag 属性上放置一个断点?
点击您的ViewBag.MyData 并按F9。调试时,一旦遇到断点,将鼠标悬停在ViewBag 上即可查看其内容。