ASP.NET MVC 4复杂的模型验证 - 模型不验证服务器端 [英] ASP.NET MVC 4 validation on complex models - model does not validated on server side

查看:130
本文介绍了ASP.NET MVC 4复杂的模型验证 - 模型不验证服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是类似于<一href=\"http://stackoverflow.com/questions/18087358/asp-net-mvc-4-unobtrusive-validation-broken-on-complex-models\">this问题但我仍然有验证服务器端和客户端的问题。我想进行比较的两个属性,在不同的模型设定。

我的模型如下:

 公共类用户{
  公共字符串密码{搞定;组; }
}公共类UserRegisterViewModel {
    公众用户用户{搞定;组; }    //这个建议在链接的问题 - 因为比较只能与本地产业合作
    公共字符串密码
    {
        {返回this.User.Password;}
    }    [数据类型(DataType.Password)
    [比较(密码的ErrorMessage =密码必须匹配)]
    [必需(的ErrorMessage =确认需要输入密码)]
    [显示名称(确认密码)]
    公共字符串CPassword {搞定;组; }
}

我的控制器动作是:

  [HttpPost]
[使用AllowAnonymous]
[ValidateAntiForgeryToken]
公众的ActionResult寄存器(UserRegisterViewModel模型)
{
    如果(ModelState.IsValid)//此条件是假的
    {
    }
    返回查看();
}

据重定向到再次验证错误说密码必须匹配注册页面。是否有人可以帮忙吗?我已经检查<一个href=\"http://stackoverflow.com/questions/18087358/asp-net-mvc-4-unobtrusive-validation-broken-on-complex-models\">this 的问题,它帮助了一点,但不是全部。

如果我改变模型结构如下图所示,比我得到一个错误说:找不到一个名为user.password的属性

 公共类UserRegisterViewModel {
    公众用户用户{搞定;组; }    [数据类型(DataType.Password)
    [比较(user.password的的ErrorMessage =密码必须匹配)]
    [必需(的ErrorMessage =确认需要输入密码)]
    [显示名称(确认密码)]
    公共字符串CPassword {搞定;组; }
}

修改

我CSHTML页code是像下面。

 &LT; P&GT;
    @ Html.LabelFor(型号=&GT; model.User.Password)
    @ Html.PasswordFor(型号=&GT; model.User.Password,新{@class =wd189 inputText的})
    @ Html.ValidationMessageFor(型号=&GT; model.User.Password)
&所述; / P&GT;
&所述p为H.;
    @ Html.LabelFor(型号=&GT; model.CPassword)
    @ Html.PasswordFor(型号=&GT; model.CPassword,新{@class =wd189 inputText的})
    @ Html.ValidationMessageFor(型号=&GT; model.CPassword)
&所述; / P&GT;


解决方案

这为我工作(适用于:客户端和服务器端)

 公共类UserRegisterViewModel
{
    私人用户_user;    公众用户用户
    {
        {返回_user =(_user ??新用户()); }
    }    公共字符串密码
    {
        {返回user.password的; }
        集合{user.password的=价值; }
    }    [数据类型(DataType.Password)
    [System.Web.Mvc.Compare(密码的ErrorMessage =密码必须匹配)]
    [必需(的ErrorMessage =确认需要输入密码)]
    [显示名称(确认密码)]
    公共字符串CPassword {搞定;组; }
}

修改

显然,认为必须如下所示:

 &LT; P&GT;
    @ Html.LabelFor(型号=&GT; model.Password)
    @ Html.PasswordFor(型号=&GT; model.Password,新{@class =wd189 inputText的})
    @ Html.ValidationMessageFor(型号=&GT; model.Password)
&所述; / P&GT;
&所述p为H.;
    @ Html.LabelFor(型号=&GT; model.CPassword)
    @ Html.PasswordFor(型号=&GT; model.CPassword,新{@class =wd189 inputText的})
    @ Html.ValidationMessageFor(型号=&GT; model.CPassword)
&所述; / P&GT;

My problem is similar to this question but I'm still having problem with validation both server- and client-side. I want to perform a compare on two properties, set in different models.

My models are as follows:

public class User{
  public string Password { get; set; }
}

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

    //This is suggested in linked question - as Compare can only work with local property
    public string Password 
    {
        get{return this.User.Password;}
    }

    [DataType(DataType.Password)]
    [Compare("Password", ErrorMessage = "Passwords must match")]
    [Required(ErrorMessage = "Confirm password is required")]
    [DisplayName("Confirm Password")]
    public string CPassword { get; set; }
}

My Controller action is:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(UserRegisterViewModel model)
{
    if (ModelState.IsValid) //This conditions is false
    {
    }
    return View(); 
}

It redirects to again register page with the validation error saying Password must match. Can someone please help ? I have checked this question it helped a bit, but not completely.

If I change the model structure like below, than I get an error saying: Could not find a property named User.Password:

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

    [DataType(DataType.Password)]
    [Compare("User.Password", ErrorMessage = "Passwords must match")]
    [Required(ErrorMessage = "Confirm password is required")]
    [DisplayName("Confirm Password")]
    public string CPassword { get; set; }
}

EDIT

My cshtml page code is like below.

<p>
    @Html.LabelFor(model => model.User.Password)
    @Html.PasswordFor(model => model.User.Password, new { @class = "wd189 inputtext" })
    @Html.ValidationMessageFor(model => model.User.Password)
</p>
<p>
    @Html.LabelFor(model => model.CPassword)
    @Html.PasswordFor(model => model.CPassword, new { @class = "wd189 inputtext" })
    @Html.ValidationMessageFor(model => model.CPassword)
</p>

解决方案

This worked for me (for both: client and server sides).

public class UserRegisterViewModel
{
    private User _user;

    public User User
    {
        get { return _user = (_user ?? new User()); }
    }

    public string Password
    {
        get { return User.Password; }
        set { User.Password = value; }
    }

    [DataType(DataType.Password)]
    [System.Web.Mvc.Compare("Password", ErrorMessage = "Passwords must match")]
    [Required(ErrorMessage = "Confirm password is required")]
    [DisplayName("Confirm Password")]
    public string CPassword { get; set; }
}

EDIT:

and obviously, the view must be as follows:

<p>
    @Html.LabelFor(model => model.Password)
    @Html.PasswordFor(model => model.Password, new { @class = "wd189 inputtext" })
    @Html.ValidationMessageFor(model => model.Password)
</p>
<p>
    @Html.LabelFor(model => model.CPassword)
    @Html.PasswordFor(model => model.CPassword, new { @class = "wd189 inputtext" })
    @Html.ValidationMessageFor(model => model.CPassword)
</p>  

这篇关于ASP.NET MVC 4复杂的模型验证 - 模型不验证服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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