为什么我的剃刀标签似乎需要一个模型属性才能显示ViewBag文本?

时间:2022-04-19 20:14:38

in an MVC 4 application this works:

在MVC 4应用程序中,这有效:

@if (ViewBag.AdvisoryMessage != null)
    {
        @Html.LabelFor(m => m.CourseApplication.CourseReason,   (string)ViewBag.AdvisoryMessage )
    }

but this doesn't:

但这不是:

@if (ViewBag.AdvisoryMessage != null)
{
    @Html.Label((string)ViewBag.AdvisoryMessage ))
}

I'd really like to know why. The text in AdvisoryMessage is page-specific and doesn't relate to CourseApplication.CourseReason.

我真的很想知道为什么。 AdvisoryMessage中的文本是特定于页面的,与CourseApplication.CourseReason无关。

Thanks!

1 个解决方案

#1


0  

A Label is related to something... so it has a "for" attribute.

标签与某些东西有关......所以它有一个“for”属性。

Html.Label(string expression, string labelText, IDictionary<string, object> htmlAttributes);

expression - Populates the "for" attribute for your label (indicating what it is for). This will also populate the content of the Label if labelText is not present

expression - 填充标签的“for”属性(指示其用途)。如果labelText不存在,这也将填充Label的内容

labelText - This populates the content / text for the Label

labelText - 填充Label的内容/文本

htmlAttributes - This defines all of the specific styles or attributes that you want to add to the element such as class, id, data- attributes, style and more.

htmlAttributes - 它定义了要添加到元素的所有特定样式或属性,例如class,id,data-attributes,style等。

Check this uses:

检查这个用途:

@Html.Label("Name")
<label for="Name">
   Name
</lable>

@Html.Label("Name", "First Name")
<label for="Name">
   First Name
</lable>

#1


0  

A Label is related to something... so it has a "for" attribute.

标签与某些东西有关......所以它有一个“for”属性。

Html.Label(string expression, string labelText, IDictionary<string, object> htmlAttributes);

expression - Populates the "for" attribute for your label (indicating what it is for). This will also populate the content of the Label if labelText is not present

expression - 填充标签的“for”属性(指示其用途)。如果labelText不存在,这也将填充Label的内容

labelText - This populates the content / text for the Label

labelText - 填充Label的内容/文本

htmlAttributes - This defines all of the specific styles or attributes that you want to add to the element such as class, id, data- attributes, style and more.

htmlAttributes - 它定义了要添加到元素的所有特定样式或属性,例如class,id,data-attributes,style等。

Check this uses:

检查这个用途:

@Html.Label("Name")
<label for="Name">
   Name
</lable>

@Html.Label("Name", "First Name")
<label for="Name">
   First Name
</lable>