该输入转换为大写字母大写的属性 [英] Uppercase attribute that converts the input to uppercase

查看:111
本文介绍了该输入转换为大写字母大写的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC4工作,要定义使用大写属性的模型。这个想法将是大写的属性的presence会导致模型值转换为大写,当它到达服务器。

I am working in MVC4 and want to define a model using an Uppercase attribute. The idea would be that the presence of the Uppercase attribute would cause the model value to be converted to uppercase when it arrived at the server.

此刻,我有以下的code中的模型:

At the moment I have the following code within the model:

    [Required]
    [Display(Name="Account Code")]
    [StringValidation(RegExValidation.AccountCode, Uppercase=true)]
    public string Account
    {
        get { return _account; }
        set
        {
            if (value != null)
                _account = value.ToUpper();
        }
    }

但我真的希望是这样的:

But what I would really like is this:

    [Required]
    [Display(Name="Account Code")]
    [StringValidation(RegExValidation.AccountCode)]
    [Uppercase]
    public string Account { get; set; }

我想我可能需要创建大写属性作为 ValidationAttribute 来确保当模型击中服务器时,它被炒鱿鱼。但是,这似乎有点错了,因为我真的不验证数据。有没有更好的办法?

I think that I may need to create the Uppercase attribute as a ValidationAttribute to ensure it gets fired when the model hits the server. But that seems a bit wrong, as I'm not really validating the data. Is there a better way?

此外,有没有什么办法,以确保属性调用顺序?我真的想自定义 StringValidation 属性触发之前为大写转换数据,因为这会检查正则表达式文本的情况。

Also, is there any way to ensure the invocation order on the attributes? I really want to convert the data to uppercase before the custom StringValidation attribute fires, as this checks the case of the text in the regex pattern.

要有点背景,加入到这一点,我想减少需要添加code为大写的数据。必杀技是一个属性,这将更新的方式进入服务器的数据,无论是在模型绑定或验证阶段。该属性然后可以在 StringValidation 属性修改在检查中使用的正则表达式值引用。我还可以再查找在自定义这个属性 TextBoxFor helper方法,这样我可以添加文本转换:大写所以看起来在客户端正确的。

To add a bit of background to this, I want to reduce the need to add code to uppercase the data. The nirvana would be a single attribute, which updates the data on the way into the server, either in the model binding or validation stage. This attribute can then be referenced in the StringValidation attribute to amend the RegEx value used in its checks. I can also then lookup this attribute in a custom TextBoxFor helper method, such that I can add text-transform: uppercase so it looks correct on the client side.

没有任何人有任何想法了吗?

Does anyone have any ideas out there?

推荐答案

我设法得到这个工作,到一个点,所以这里是我给别人评价的解决方案。

I have managed to get this working, to a point, so here's my solution for others to appraise.

在一点要注意的是,完整的解决方案无法实现,因为我无法让 Modelmetadata 中的 StringValidation.IsValid( )属性。具体问题我在这里的是,我能得到的元数据,但是我无法让属性名从中,只有显示名称。有多种选择摆在那里,但事实证明,我的一些属性具有相同的显示名称意味着我不能肯定的是, ProprtyName 是一个我实际上验证。

Once point to note was that the full solution couldn't be achieved because I couldn't get the Modelmetadata inside the StringValidation.IsValid() attribute. The particular issue I had here was that I could get the Metadata, however I could not get the PropertyName from it, only the DisplayName. There were multiple options out there, but the fact that some of my properties have the same DisplayName means that I couldn't be sure that the ProprtyName was the one I was actually validating.

这里的code为 ValidationAttribute

public class StringValidationAttribute : ValidationAttribute, IClientValidatable, IMetadataAware {

    private bool _uppercase;

    public StringValidationAttribute(bool uppercase = false) {
         _uppercase = uppercase;
    }

    ...

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues["Uppercase"] = _uppercase;
    }
}

然后,我创建了一个新的 IModelBinder 实施

public class StringBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {           
        ValueProviderResult result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (result == null)
            return null;

        if (bindingContext.ModelMetadata.AdditionalValues.ContainsKey("Uppercase")) {
            if ((bool)bindingContext.ModelMetadata.AdditionalValues["Uppercase"]])
                return result.AttemptedValue.ToUpper();
        }

        return result.AttemptedValue;
    }
}

和登记,在我的的Global.asax 文件:

ModelBinders.Binders.Add(typeof(string), new StringBinder());

在code到目前为止将导致任何字符串输入进入MVC转换为大写,如果有 StringValidationAttribute 连接到它的模型,其中,大写的指标已经设定。

The code so far will cause any string input coming into MVC to be converted to Uppercase if it has StringValidationAttribute attached to it on the model, and where the uppercase indicator has been set.

其次,要实现我的制作HTML表单的欲望过于大写,我实现了一个新的 EditorTemplate 名为 string.cshtml 。根据这种观点我说:

Next, to achieve my desire of making the html forms be uppercase too, I implemented a new EditorTemplate named string.cshtml. In this view I added:

RouteValueDictionary htmlAttributes = new RouteValueDictionary();
if ((bool)ViewData.ModelMetadata.AdditionalValues["Uppercase"]) {
    htmlAttributes.Add("class", "Uppercase");
}
@Html.TextBox("", Model, htmlAttributes)

随着CSS的;

With the CSS as;

.Uppercase {
    text-transform: uppercase;
}

希望这篇文章可以帮助一些人在那里。

Hope this post helps some others out there.

这篇关于该输入转换为大写字母大写的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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