MVC-3 和 Dynamic - @Html.Label(View.X) 不渲染
使用MVC-3,Razor:
-- MyController --
public ActionResult Index(String message) // where message = "hello"
{
ViewModel.Test1 = "This is a test";
ViewModel.Test2 = "This is another test. " + message;
}
-- Index.cshtml --
@Html.Label((string)View.Test1)
<br />
@Html.Label((string)View.Test2)
为什么它只会渲染出以下内容?
<label for="This is a test">This is a test</label>
<br />
在过去的几天里,这让我简直要疯了,而且似乎毫无意义。这一定是有原因的。
我可以调试它并逐步浏览这些视图。在视图中,我观察到该行已被处理,并且 View.Test2 的值是“这是另一个测试。你好”。
我在某些情况下正在执行以下操作并且效果很好。
(例)
ViewModel.Something = this.readDataService.GetSomething();
有什么区别?
谢谢,
罗布
Using MVC-3, Razor:
-- MyController --
public ActionResult Index(String message) // where message = "hello"
{
ViewModel.Test1 = "This is a test";
ViewModel.Test2 = "This is another test. " + message;
}
-- Index.cshtml --
@Html.Label((string)View.Test1)
<br />
@Html.Label((string)View.Test2)
Why will it only render out the following?
<label for="This is a test">This is a test</label>
<br />
It's been driving me absolutely crazy over the past few days and seems to make no sense. There has to be a reason for it.
I can debug this and step through thew view. In the view, I watch as this line is processed and the value of View.Test2 is "This is another test. hello".
I have cases where I am doing the following and it works fine.
(ex)
ViewModel.Something = this.readDataService.GetSomething();
What's the difference?
Thanks,
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在使用 ASP.NET MVC 3 的 RC2 之前版本。
ViewModel
在 RC 2 中更改为ViewBag
(请参阅 这篇文章 作者:Scott格思里)。看起来您确实正在尝试使用 ViewModel 作为视图的强类型模型。相反,创建一个类用作模型,然后使用
@Html.LabelFor
:在控制器中:
在视图中:
呈现:
HTH
Looks like you are using a pre-RC2 version of ASP.NET MVC 3.
ViewModel
was changed toViewBag
in RC 2 (see the this post by Scott Guthrie).And it does look like you are trying to use
ViewModel
as the strongly typed model for your view. Instead, create a class to use as your model and then use@Html.LabelFor
:in the controller:
in the view:
which renders:
HTH