C# 当MVC代码首先与数据注释一起使用时,如何在显示名称中包含html标记?

C# 当MVC代码首先与数据注释一起使用时,如何在显示名称中包含html标记?,c#,asp.net-mvc,asp.net-mvc-4,code-first,razor-2,C#,Asp.net Mvc,Asp.net Mvc 4,Code First,Razor 2,我正在将纸质表单转换为MVC4 web表单。我的问题是一段文字,其中包括链接到脚注的上标数字。这就是我想做的: public class PaperFormModel { [Display(Name = "Full paragraph of question text copied straight off of the paper form<a href="#footnote1"><sup>footnote 1</sup><

我正在将纸质表单转换为MVC4 web表单。我的问题是一段文字,其中包括链接到脚注的上标数字。这就是我想做的:

public class PaperFormModel
{
    [Display(Name = "Full paragraph of question text copied straight 
         off of the paper form<a href="#footnote1"><sup>footnote 1</sup></a> 
         and it needs to include the properly formatted superscript 
         and/or a link to the footnote text.")]
    public string Question1 { get; set; }

    // more properties go here ...
}
公共类PaperFormModel
{
[显示(Name=“问题文本的完整段落已直接复制
纸外表格
它需要包含正确格式的上标
和/或脚注文本的链接。”)]
公共字符串问题1{get;set;}
//这里有更多的财产。。。
}
创建模型后,我生成了控制器和相关视图。除了显示名称中的html标记外,其他所有内容都可以转换为html编码文本(即sup1/sup)。view.cshtml中用于显示属性的代码就是自动生成的代码:

<div class="editor-label">
    @Html.LabelFor(model => model.Question1)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Question1)
    @Html.ValidationMessageFor(model => model.Question1)
</div>

@LabelFor(model=>model.Question1)
@EditorFor(model=>model.Question1)
@Html.ValidationMessageFor(model=>model.Question1)

我正在试图弄清楚如何让脚注的html标记正常工作,或者我的方法是错误的,我应该用另一种方式来做?这是我的第一个MVC项目,我来自asp.net背景。

我认为您应该尝试将HTML文本移动到参考资料中,并为您的模型应用下一个代码:

public class PaperFormModel
{    
    [Display(ResourceType = typeof(PaperFormModelResources), Name = "Question1FieldName")]
    public string Question1 { get; set; }

    // more properties go here ...
}
创建资源文件:
-在解决方案中创建
Resources
文件夹(如果不存在)。
-
右键单击解决方案资源管理器中的此文件夹->添加->资源文件
…->添加->新项目
并选择资源文件
-将此文件命名为PaperFormModelResources
-添加名为
Question1FieldName
且值为
的新条目,问题文本的完整段落直接从纸质表单复制而来,需要包含格式正确的上标和/或脚注文本链接。
使用资源管理器

编辑: 如果html标记显示不正确(仅显示为纯文本),您可以使用以下答案:


@Html.Raw(HttpUtility.HtmlDecode(Html.LabelFor(model=>model.Question1.ToHtmlString))
@EditorFor(model=>model.Question1)
@Html.ValidationMessageFor(model=>model.Question1)

希望有帮助。

如果你把“@Html.Raw(HttpUtility…”部分放在自己的答案中,我会把它标记为答案。@Bakanekobrain,我已经在我的答案中应用了这些更改以适应你的问题:
@Html.Raw(HttpUtility.HtmlDecode(Html.LabelFor(model=>model.Question1.ToHtmlString))
ToHtmlString是一个方法调用,因此需要括号。
<div class="editor-label">
    @Html.Raw(HttpUtility.HtmlDecode(Html.LabelFor(model => model.Question1).ToHtmlString))
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Question1)
    @Html.ValidationMessageFor(model => model.Question1)
</div>