从 C# MVC 模型数据直接 HTML 显示到 CSHTML 页面?或者在服务器端添加 HTML 换行符?
Display straight HTML from C# MVC model data to CSHTML page? or add HTML line breaks server side?
有没有办法将 C# MVC 模型数据直接 HTML 显示到 CSHTML 页面?或者在服务器端添加 HTML 换行符?
有没有一种方法可以使用 @Html.DisplayNameFor()
从 C# MVC 模型数据中直接显示 HTML 或者是否有 @Html.DisplayNameAsHTMLFor()
方法?
模型有以下内容:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc.Rendering;
public class PersonObjModel
{
[Required]
[Display(Name = "I AGREE")]
public bool IAgree { get; set; }
[Required]
[Display(Name = "<br />Welcome! <br />What is your name?")]
public string Name { get; set; }
}
如何在我的网页中显示这些 HTML 中断,
使其不将 HTML 显示为文本?
.CSHTML 页面有以下内容:
@model HoldemCore.Models.PersonObjModel
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
@Html.DisplayNameFor(model => model.IAgree)
</div>
<div class="display-field">
<input type="checkbox" id="IAgree" name="IAgree"
@if (Model.IAgree) { "checked value=\"true\"".ToString(); }
@if (!Model.IAgree) { "value=\"false\"".ToString(); }
onchange="this.value = this.checked;" />
</div>
<div class="editor-label">
@Html.DisplayNameFor(model => model.Name)
</div>
<div class="display-field">
@Html.EditorFor(model => model.Name)
</div>
</fieldset>
}
选中的值未在源中正确显示,并且 HTML 在 [Display(Name] for [Name] 字段
中显示为文本
.CSHTML 页面有以下.. @Html.Raw() 是答案
@model HoldemCore.Models.PersonObjModel
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
@Html.DisplayNameFor(model => model.IAgree)
</div>
<div class="display-field">
<input type="checkbox" id="IAgree" name="IAgree"
@if (Model.IAgree) { @Html.Raw("checked value=\"true\""); }
@if (!Model.IAgree) { @Html.Raw("value=\"false\""); }
onchange="this.value = this.checked;" />
</div>
<div class="editor-label">
@Html.Raw(@Html.DisplayNameFor(model => model.Name))
</div>
<div class="display-field">
@Html.EditorFor(model => model.Name)
</div>
</fieldset>
}
.CSHTML 页面有以下.. @Html.Raw() 是答案
@model HoldemCore.Models.PersonObjModel
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
@Html.DisplayNameFor(model => model.IAgree)
</div>
<div class="display-field">
<input type="checkbox" id="IAgree" name="IAgree"
@if (Model.IAgree) { @Html.Raw("checked value=\"true\""); }
@if (!Model.IAgree) { @Html.Raw("value=\"false\""); }
onchange="this.value = this.checked;" />
</div>
<div class="editor-label">
@Html.Raw(@Html.DisplayNameFor(model => model.Name))
</div>
<div class="display-field">
@Html.EditorFor(model => model.Name)
</div>
</fieldset>
}
有没有办法将 C# MVC 模型数据直接 HTML 显示到 CSHTML 页面?或者在服务器端添加 HTML 换行符?
有没有一种方法可以使用 @Html.DisplayNameFor()
从 C# MVC 模型数据中直接显示 HTML 或者是否有 @Html.DisplayNameAsHTMLFor()
方法?
模型有以下内容:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc.Rendering;
public class PersonObjModel
{
[Required]
[Display(Name = "I AGREE")]
public bool IAgree { get; set; }
[Required]
[Display(Name = "<br />Welcome! <br />What is your name?")]
public string Name { get; set; }
}
如何在我的网页中显示这些 HTML 中断,
使其不将 HTML 显示为文本?
.CSHTML 页面有以下内容:
@model HoldemCore.Models.PersonObjModel
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
@Html.DisplayNameFor(model => model.IAgree)
</div>
<div class="display-field">
<input type="checkbox" id="IAgree" name="IAgree"
@if (Model.IAgree) { "checked value=\"true\"".ToString(); }
@if (!Model.IAgree) { "value=\"false\"".ToString(); }
onchange="this.value = this.checked;" />
</div>
<div class="editor-label">
@Html.DisplayNameFor(model => model.Name)
</div>
<div class="display-field">
@Html.EditorFor(model => model.Name)
</div>
</fieldset>
}
选中的值未在源中正确显示,并且 HTML 在 [Display(Name] for [Name] 字段
中显示为文本.CSHTML 页面有以下.. @Html.Raw() 是答案
@model HoldemCore.Models.PersonObjModel
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
@Html.DisplayNameFor(model => model.IAgree)
</div>
<div class="display-field">
<input type="checkbox" id="IAgree" name="IAgree"
@if (Model.IAgree) { @Html.Raw("checked value=\"true\""); }
@if (!Model.IAgree) { @Html.Raw("value=\"false\""); }
onchange="this.value = this.checked;" />
</div>
<div class="editor-label">
@Html.Raw(@Html.DisplayNameFor(model => model.Name))
</div>
<div class="display-field">
@Html.EditorFor(model => model.Name)
</div>
</fieldset>
}
.CSHTML 页面有以下.. @Html.Raw() 是答案
@model HoldemCore.Models.PersonObjModel
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
@Html.DisplayNameFor(model => model.IAgree)
</div>
<div class="display-field">
<input type="checkbox" id="IAgree" name="IAgree"
@if (Model.IAgree) { @Html.Raw("checked value=\"true\""); }
@if (!Model.IAgree) { @Html.Raw("value=\"false\""); }
onchange="this.value = this.checked;" />
</div>
<div class="editor-label">
@Html.Raw(@Html.DisplayNameFor(model => model.Name))
</div>
<div class="display-field">
@Html.EditorFor(model => model.Name)
</div>
</fieldset>
}