MVC远程属性附加字段 [英] MVC Remote Attribute Additional Fields

查看:182
本文介绍了MVC远程属性附加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的远程验证加上输入字段名(mymodel.field1)的preFIX在数据-VAL-远程additionalfields列出的每个附加字段。在我的附加字段我有一个隐藏字段不是模型的一部分​​,所以它有像fieldhidden而不是mymodel.fieldhidden一个正常的名称。

我已经在控制器审查请求对象确认这并验证该查询字符串键中的一个被mymodel.fieldhidden而不是fieldhidden,数据为空。 pretty确保其空,因为jQuery验证正在寻找mymodel.fieldhidden,当然找不到了。

有没有办法让jQuery的,通过MVC的属性,不会自动preFIX或通过jQuery的操纵无法自动preFIX附加字段所在的name属性的格式有像model.fieldname里的值远程验证加入?

  [远程(ValidationMethod,控制器,AdditionalFields =FieldNameInModel,ElementNameNotInModel)]
    公共字符串FieldToRemoteValidate {搞定;组; }


解决方案

我最终什么不得不做的是添加隐藏字段的两倍。我已经在控制器期待的名称是多个动作的通用设置。因此,通过与命名约定了jQuery验证库预期将隐藏字段,然后将其作为查询字符串的一部分传输。命名约定是Model_Field id属性和Model.Field name属性。

然后我用用preFIX绑定参数属性如下:

  [装订(preFIX =Model.FieldToRemotevalidate)]串FieldToRemoteValidate

The jquery remote validation is adding the prefix of the input field name (mymodel.field1) to each additional field listed in data-val-remote-additionalfields. In my additional fields I have a hidden field that is not part of the model so it has a normal name like "fieldhidden" instead of "mymodel.fieldhidden".

I have confirmed this by reviewing the request object at the controller and verified that one of the query string keys is "mymodel.fieldhidden" instead of "fieldhidden" and the data is null. Pretty sure its null because jquery validation is looking for "mymodel.fieldhidden" and of course can't find it.

Is there a way to make jquery, through mvc attributes, not auto prefix or through jquery manipulation to not auto prefix the additional fields where the name attribute has a value formatted like "model.fieldname" where remote validation is added ?

    [Remote("ValidationMethod", "Controller", AdditionalFields = "FieldNameInModel, ElementNameNotInModel")]
    public string FieldToRemoteValidate { get; set; }

解决方案

What I ended up having to do was add the hidden field twice. I already had a generic setup for multiple actions in the controller expecting the name as was. So by adding the hidden field with the naming convention that the jquery validation library expected, it was then transmitted as part of the query string. The naming convention is Model_Field for the id attribute and Model.Field for the name attribute.

I then used the bind parameter attribute with the prefix as follows:

[Bind(Prefix = "Model.FieldToRemotevalidate")] string FieldToRemoteValidate

这篇关于MVC远程属性附加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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