【问题标题】:How to add custom model field annotations to render in the view ASP.NET, Core 3, MVC如何添加自定义模型字段注释以在视图中呈现 ASP.NET、Core 3、MVC
【发布时间】:2020-07-27 13:38:16
【问题描述】:

是否可以,如果可以,如何添加自定义模型字段注释以在视图中以特定方式呈现?

快速示例:

这是一个简单的 ContactUs 模型类:

namespace Core.Business.Models
{
    public class ContactUsModel
    {
        public string Name { get; set; }
        public string EmailAddress { get; set; }
        public string Telephone { get; set; }
        public string Message { get; set; }
    }
}

然后在视图中呈现如下:

<div asp-validation-summary="ModelOnly" class="text-danger"></div>

<div class="form-group row">
    <label asp-for="Name" class="col-sm-3 control-label"></label>
    <div class="col-sm-9">
        <input asp-for="Name" class="form-control" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
</div>
<div class="form-group row">
    <label asp-for="EmailAddress" class="col-sm-3 control-label"></label>
    <div class="col-sm-9">
        <input asp-for="EmailAddress" class="form-control" />
        <span asp-validation-for="EmailAddress" class="text-danger"></span>
    </div>
</div>
<div class="form-group row">
    <label asp-for="Telephone" class="col-sm-3 control-label"></label>
    <div class="col-sm-9">
        <input asp-for="Telephone" class="form-control" />
        <span asp-validation-for="Telephone" class="text-danger"></span>
    </div>
</div>
<div class="form-group row">
    <label asp-for="Message" class="col-sm-3 control-label"></label>
    <div class="col-sm-9">
        <textarea rows="8" asp-for="Message" class="form-control"></textarea>
        <span asp-validation-for="Message" class="text-danger"></span>
    </div>
</div>
<input type="submit" value="Send Message" class="btn btn-primary" />

现在,我真正想要的是能够向模型类添加自定义注释,以在预先设计的庄园中呈现:

例如:ContactUs 模型类:

namespace Core.Business.Models
{
    public class ContactUsModel
    {
        public string Name { get; set; }
        public string EmailAddress { get; set; }
        public string Telephone { get; set; }

        [CustomTag(Description="Please enter your message here, the more information you can provide the better.")]
        public string Message { get; set; }
    }
}

然后在视图上我可以使用“标签助手”来显示这个注释:

<div class="form-group row">
    <label asp-for="Message" class="col-sm-3 control-label"></label>
    <div class="col-sm-9">
        <textarea rows="8" asp-for="Message" class="form-control"></textarea>

        **@html.MyTag(...here would be pulled in the custom tag annotation value....)**

        <span asp-validation-for="Message" class="text-danger"></span>
    </div>
</div>

我的想法是,我可以用“帮助”信息来注释模型类,这些信息可以在页面上呈现为帮助文本或“i”图标,单击时会弹出帮助文本。

可能吗?

谢谢

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc asp.net-core-3.1


    【解决方案1】:

    自定义标签助手在这里有用吗?

    当您需要尚未定义的布局或属性时,您可以创建自定义标签助手 - 例如引导卡

    还可以通过基于锚标签助手创建自定义标签助手来扩展现有标签助手,例如锚。

    您还可以创建一个基类标签助手,它具有由所有自定义标签助手共享的通用功能

    一个简单的例子

    /// <summary>
    /// Render simple text : <textonly resource-name="RegisteredAddress" resource-list="Model" class="text-dark font-weight-bold"></textonly>
    /// </summary>
    public class TextonlyTagHelper : TagHelperEx
    {
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            // we need a resource in order to proceed
            if (this.Resource != null)
            {
                // render a html span
                output.TagName = "span";
    
                // with this tooltip
                output.Attributes.SetAttribute("title", this.Resource.Tooltip);
    
                // and this text
                output.Content.SetContent(this.Resource.Title);
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。我会看看我是否可以查看您的自定义标签助手扩展示例,看看这是否解决了我的问题。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2022-07-18
    • 1970-01-01
    相关资源
    最近更新 更多