访问模型一个辅助扩展类属性 [英] Accessing the model attributes in a helper extension class

查看:147
本文介绍了访问模型一个辅助扩展类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建一个文本框禁用如果属性 是present模型中的一个属性。

Trying to create a disabled textbox if the attribute is present on a property in the model.

public static IHtmlString SmartTextBox(this HtmlHelper helper, string content)
{
     string htmlString = String.Format("<input type="text">{0}</input>", content);
     return new HtmlString(htmlString);
}

型号:

public class User
{        
    public int Age { get; set; }

    [Editable(false)]
    public string Name { get; set; }
}

反正是有检查这里的模型,然后,如果它被禁用残疾人属性添加到输入元素?

Is there anyway to check the model here and then add the disabled attribute to the input element if it's been disabled?

推荐答案

下面是一个帮助我写道,增加了一个*为标签,如果需要作为模型属性标记。 ModelMetaData有IsReadOnly属性为可能能够利用。您应该能够作出正确的换人使一个文本框的变化。

Here's a helper I wrote that adds an '*' to a label if the model property is marked as required. ModelMetaData has an IsReadonly property to might be able to leverage. You should be able to make the correct substitutions to make the changes for a textbox.

public static class LabelExtensions
{
    public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
        Expression<Func<TModel, TValue>> expression, IDictionary<String, Object> htmlAttributes,
        String requiredMarker = "*")
    {
        return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData),
            ExpressionHelper.GetExpressionText(expression), null, htmlAttributes, requiredMarker);
    }

    public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
        Expression<Func<TModel, TValue>> expression, Object htmlAttributes, String requiredMarker)
    {
        return LabelFor(html, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), requiredMarker);
    }

    internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName,
        String labelText = null, IDictionary<String, Object> htmlAttributes = null, String requiredMarker = null)
    {
        var resolvedLabelText = labelText ??
                                metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();

        var tag = new TagBuilder("label");
        tag.Attributes.Add("for",
            TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
        tag.SetInnerText(resolvedLabelText);
        tag.MergeAttributes(htmlAttributes, true);

        if (metadata.IsRequired && !String.IsNullOrWhiteSpace(requiredMarker))
        {
            var requiredSpan = new TagBuilder("span") {InnerHtml = requiredMarker};
            requiredSpan.AddCssClass("required");

            tag.InnerHtml += requiredSpan;
        }

        var result = tag.ToString(TagRenderMode.Normal);

        return new MvcHtmlString(result);
    }
}

这篇关于访问模型一个辅助扩展类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆