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

时间:2022-01-08 15:18:34

I am converting a paper form into a MVC 4 web form. I have questions that are a paragraph worth of text that include superscript numbers that link to footnotes. This is what I am trying to do:

我正在将纸质表单转换为MVC 4 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 ...
}

After creating the model I generated the controller and related views. Everything works except the html markup in the display name is converted into html encoded text (i.e. &lt;sup&gt;1&lt;/sup&gt;). The code in the view.cshtml used to display the property is just the automatically generated code:

在创建模型之后,我生成了控制器和相关视图。除了显示名称中的html标记被转换成html编码的文本之外,所有的工作都是有效的(例如,sup>1</sup>)。视图中的代码。用于显示属性的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>

I am trying to figure out how to get the html markup for the footnotes to work properly or is my approach somehow wrong and I should be doing it a different way? This is my first MVC project and I am coming from an asp.net background.

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

1 个解决方案

#1


2  

I think you should try to move your HTML text to resources and apply for your model the next code:

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

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

    // more properties go here ...
}

To create resource file:
- Create Resources folder in your solution if it does not exist.
- Right click on this folder in solution explorer -> Add -> Resource file or ... -> Add -> New item and select resource file
- Name this file PaperFormModelResources
- Add new entry with name Question1FieldName and with value 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. using resource manager.

要创建资源文件:-在你的解决方案中创建资源文件夹,如果它不存在。-右键单击解决方案资源管理器->添加->资源文件或…- >添加- >新项目并选择资源文件,这个文件名字PaperFormModelResources——添加新条目名称Question1FieldName和价值的完整段问题文本复制直接纸质表单< a href = " # footnote1 " > <一口> 脚注1 < /一口> < / >,它需要包括正确格式化的上标和/或脚注文本的链接。使用资源管理器。

EDITS: If, as result, your html markup is not displayed correctly (it is just displayed as plain text) you can use the answer for this question that is:

编辑:如果您的html标记没有正确显示(它只是显示为纯文本),您可以使用这个问题的答案:

<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>

Hope it helped.

希望它帮助。

#1


2  

I think you should try to move your HTML text to resources and apply for your model the next code:

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

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

    // more properties go here ...
}

To create resource file:
- Create Resources folder in your solution if it does not exist.
- Right click on this folder in solution explorer -> Add -> Resource file or ... -> Add -> New item and select resource file
- Name this file PaperFormModelResources
- Add new entry with name Question1FieldName and with value 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. using resource manager.

要创建资源文件:-在你的解决方案中创建资源文件夹,如果它不存在。-右键单击解决方案资源管理器->添加->资源文件或…- >添加- >新项目并选择资源文件,这个文件名字PaperFormModelResources——添加新条目名称Question1FieldName和价值的完整段问题文本复制直接纸质表单< a href = " # footnote1 " > <一口> 脚注1 < /一口> < / >,它需要包括正确格式化的上标和/或脚注文本的链接。使用资源管理器。

EDITS: If, as result, your html markup is not displayed correctly (it is just displayed as plain text) you can use the answer for this question that is:

编辑:如果您的html标记没有正确显示(它只是显示为纯文本),您可以使用这个问题的答案:

<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>

Hope it helped.

希望它帮助。