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
Looks like you are using a pre-RC2 version of ASP.NET MVC 3.
ViewModelwas changed toViewBagin RC 2 (see the this post by Scott Guthrie).And it does look like you are trying to use
ViewModelas 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