MVC-3 和 Dynamic - @Html.Label(View.X) 不渲染

发布于 2024-10-20 04:56:40 字数 783 浏览 0 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2024-10-27 04:56:40

看起来您正在使用 ASP.NET MVC 3 的 RC2 之前版本。ViewModel 在 RC 2 中更改为 ViewBag(请参阅 这篇文章 作者:Scott格思里)。

在 ASP.NET MVC 3 的早期预览版中,我们使用 Controller 基类上名为“ViewModel”的动态属性以及视图模板中名为“View”的动态属性来公开此 API。许多人发现两个不同的名称令人困惑,还有一些人表示,在这种情况下使用 ViewModel 这个名称会令人困惑——因为您经常在 ASP.NET MVC 中创建强类型 ViewModel 类,并且它们不使用这个API。

通过 RC2,我们在控制器和视图中公开了一个具有相同名称的动态属性 – ViewBag。

看起来您确实正在尝试使用 ViewModel 作为视图的强类型模型。相反,创建一个类用作模型,然后使用 @Html.LabelFor:

public class PersonModel
{
    public string Name { get; set; }
}

在控制器中:

PersonModel model = new PersonModel { Name = "John" };
return View(model);

在视图中:

@Html.LabelFor(model => model.Name): @Html.TextBoxFor(model => model.Name)

呈现:

<label for="Name">Name</label>: <input id="Name" name="Name" type="text" value="John" />

HTH

Looks like you are using a pre-RC2 version of ASP.NET MVC 3. ViewModel was changed to ViewBag in RC 2 (see the this post by Scott Guthrie).

With earlier previews of ASP.NET MVC 3 we exposed this API using a dynamic property called “ViewModel” on the Controller base class, and with a dynamic property called “View” within view templates. A lot of people found the fact that there were two different names confusing, and several also said that using the name ViewModel was confusing in this context – since often you create strongly-typed ViewModel classes in ASP.NET MVC, and they do not use this API.

With RC2 we are exposing a dynamic property that has the same name – ViewBag – within both Controllers and Views.

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:

public class PersonModel
{
    public string Name { get; set; }
}

in the controller:

PersonModel model = new PersonModel { Name = "John" };
return View(model);

in the view:

@Html.LabelFor(model => model.Name): @Html.TextBoxFor(model => model.Name)

which renders:

<label for="Name">Name</label>: <input id="Name" name="Name" type="text" value="John" />

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文