嵌套对象的远程视图模型验证不工作 [英] Remote ViewModel validation of nested objects not working

查看:159
本文介绍了嵌套对象的远程视图模型验证不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样一类用户:

I have a class user which looks like this:

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

    [Required(ErrorMessage = "A username is required.")]
    [StringLength(20, ErrorMessage = "Your username must be 4-20 characters.", MinimumLength = 4)]
    [RegularExpression("^[a-zA-Z0-9]*$", ErrorMessage = "Your username can only consist of letters and numbers.")]
    [Remote("UsernameExists", "RemoteValidation", ErrorMessage = "Username is already taken")]
    public string Username { get; set; }

    [Required(ErrorMessage = "A password is required.")]
    [MinLength(4, ErrorMessage = "Your password must have at least 4 letters.")]
    public string Password { get; set; }

    [Required(ErrorMessage = "An email address is required.")]
    public string Email { get; set; }
}

有关我已经创建了一个拥有用户对象和确认密码字符串中的视图模型的功能注册:

For the Register functionality I have created a ViewModel that holds a User object and a string for the password confirmation:

public class RegistrationViewModel
{
    public User User { get; set; }

    [DisplayName("Password confirmation")]
    [Required, Compare("User.Password", ErrorMessage = "The password do not match")]
    public string PasswordConfirmation { get; set; }
}

我遇到的第一个问题是,我似乎无法得到比较(user.password的)来工作,因为它似乎并没有找到对用户属性的验证。有什么办法来验证对user.password的属性PasswordConfirmation财产?

The first problem I run into is that I can't seem to get the validation for Compare("User.Password") to work as it does not seem to find the property on the user. Is there any way to validate the PasswordConfirmation property against the User.Password property?

的第二个问题是用户名字段的远程验证。我跟大卫·海登的教程在<一个href=\"http://davidhayden.com/blog/dave/archive/2011/01/04/ASPNETMVC3RemoteValidationTutorial.aspx\">http://davidhayden.com/blog/dave/archive/2011/01/04/ASPNETMVC3RemoteValidationTutorial.aspx但参数的用户名在UsernameExists方法总是空。我失去了一些东西在这里?

The second problem is the Remote validation of the Username field. I followed David Hayden's tutorial at http://davidhayden.com/blog/dave/archive/2011/01/04/ASPNETMVC3RemoteValidationTutorial.aspx but the parameter username in the UsernameExists method is always null. Am I missing something here?

编辑:

我很抱歉,但我其实不是我收到的密码比较错误足够清晰。在田间地头填充时,如果密码不匹配,我会收到一个错误,它工作正常。然而,提交我得到了验证摘要以下错误表单时:无法找到一个名为UserToRegister.Password属性

I'm sorry but I was actually not clear enough on the error I receive for the password comparison. It works fine when filling in the fields, if the passwords do not match I will receive an error. However, when submitting the form I get the following error in the validation summary: Could not find a property named UserToRegister.Password.

编辑2:

我已经想通了感谢乔的职位问题的一部分。远程验证回发网址/?UserToRegister.Username =气温这显然不我控制器操作的用户名参数相匹配。为了映射我的行为参数UserToRegister.Username要求如下:

I have figured out part of the problem thanks to Joe's post. The remote validator posts back URL/?UserToRegister.Username=temp which obviously does not match the username parameter of my controller action. In order to map my action parameter to UserToRegister.Username the following is required:

public ActionResult UsernameExists([Bind(Prefix = "UserToRegister.Username")]string username)

这现在可以正确地传递参数的方法。不过,我对使用密码字段的属性比较时,仍然得到错误。

This now correctly passes the parameter to the method. However I still get the error when using the Compare attribute on the password field.

感谢。

推荐答案

通过对user.password的属性PasswordConfigurmation财产的验证的问题是由错误在jquery.validate.unobtrusive.js文件引起。

The issue with the validation of the PasswordConfigurmation property against the User.Password property is caused by a bug in in the 'jquery.validate.unobtrusive.js' file.

原来,jQuery的'equalTo'功能是:

Originally, the jquery 'equalTo' function is:

adapters.add("equalto", ["other"], function (options) {
var prefix = getModelPrefix(options.element.name),
other = options.params.other,
fullOtherName = appendModelPrefix(other, prefix),
element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

setValidationValues(options, "equalTo", element);
});

您只需要修改这一行:

element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

element = $(options.form).find(":input[name='" + fullOtherName + "']")[0];

请注意周围的'fullOtherName选择单引号加。一旦您完成了这一变化,客户端验证按预期工作。

Note the addition on the single quotes around the 'fullOtherName' selector. Once you've made this change, the client side validation works as expected.

这篇关于嵌套对象的远程视图模型验证不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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