在mvc中确认密码 [英] confirm the password in mvc

查看:96
本文介绍了在mvc中确认密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

EntityFramework.dll中出现System.Data.Entity.Validation.DbEntityValidationException类型的异常,但未在用户代码中处理



附加信息:一个或多个实体的验证失败。有关更多详细信息,请参阅''EntityValidationErrors''属性。



======================= ====================

I got this error:
An exception of type ''System.Data.Entity.Validation.DbEntityValidationException'' occurred in EntityFramework.dll but was not handled in user code

Additional information: Validation failed for one or more entities. See ''EntityValidationErrors'' property for more details.

===========================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;

namespace ParkingMvcApp.Models
{
    [MetadataType(typeof(ClientMetaData))]

    public partial class Client
    {
        [DataType("Password")]
        [System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class ClientMetaData
    {
        [StringLength(10, MinimumLength = 10, ErrorMessage = "Your mobile not corrct")]
        [RegularExpression("([1-9][0-9]*)", ErrorMessage = "You have to enter only numbers 111-111-1111")]
        [Required]
        [Remote("IsUserNameAvailabe","Account",ErrorMessage ="The mobile already in use")]
        public string Mobile { get; set; }

        [Required]
        [DataType("Password")]
        public string Password { get; set; }

        [Required]
        [DisplayName("First Name")]

        public string FirstName { get; set; }

        [DisplayName("Created Date")]
        public Nullable<System.DateTime> CreatedDate { get; set; }


    }
}





======== =============================================



[HttpPost]

public ActionResult Register(string Mobile,string Password,string FirstName,int Disability)

{

ParkingContext db = new ParkingContext();



ParkingMvcApp.Models.Client client = new Models.Client();

client.Mobile = Mobile;

client.Role =C;

client.Password =密码;

client.FirstName = FirstName ;

client.CreatedDate = DateTime.Now;

client.TyepOfClientID =残疾;



//收件人检查用户是否已经注册了

if(db.Clients.Any(m => m.Mobile == Mobile))

{

ModelState.AddModelError(移动,移动设备已经在使用);

}



if(ModelState.IsValid)

{

db.Clients.Add(client);

db.SaveChanges(); < -----------------------------------错误

返回RedirectToAction(登录,帐户);

}



返回查看(); ;



}



注意:如果我从代码中删除比较,程序就像我一样工作预期



=====================================================

[HttpPost]
public ActionResult Register(string Mobile, string Password, string FirstName, int Disability)
{
ParkingContext db = new ParkingContext();

ParkingMvcApp.Models.Client client = new Models.Client();
client.Mobile = Mobile;
client.Role = "C";
client.Password = Password;
client.FirstName = FirstName;
client.CreatedDate = DateTime.Now;
client.TyepOfClientID = Disability;

//To check if the user already regitser
if (db.Clients.Any(m => m.Mobile == Mobile))
{
ModelState.AddModelError("Mobile", "The mobile already in use");
}

if (ModelState.IsValid)
{
db.Clients.Add(client);
db.SaveChanges(); <-----------------------------------Error
return RedirectToAction("Login", "Account");
}

return View(); ;

}

Note: If I remove the compare from the code , the program work as I expected

推荐答案

你的方法在这里是错误的。不要更改View的数据模型/域模型,你可以在这里创建一个ViewModel并将它们映射到DataModel,在View中将它绑定到ViewModel,对于DB Operartions使用你的DataModel。



您不应该使用映射到View中的数据库表的域/数据模型,您应该根据View中的要求使用ViewModel,并在ViewModel上使用适当的Compare,Required等属性,而不是Domain Model。我希望它有所帮助。





Your approach is wrong here. Don''t change your Data Model /Domain Model for View, you have yo create a ViewModel here and map those to DataModel, in View bind it to ViewModel and for DB Operartions use your DataModel.

You should not use Domain/ Data Model which is mapped to database table in View, you should use ViewModel according to requirements in your View and appply Compare, Required etc attributes on ViewModel, not Domain Model. I hope it helps.


public class ClientVM
    {
        [StringLength(10, MinimumLength = 10, ErrorMessage = "Your mobile not corrct")]
        [RegularExpression("([1-9][0-9]*)", ErrorMessage = "You have to enter only numbers 111-111-1111")]
        [Required]
        [Remote("IsUserNameAvailabe","Account",ErrorMessage ="The mobile already in use")]
        public string Mobile { get; set; }
 
        [Required]
        [DataType("Password")]
        public string Password { get; set; }

        [DataType("Password")]
        [System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
 
        [Required]
        [DisplayName("First Name")]
 
        public string FirstName { get; set; }
 
        [DisplayName("Created Date")]
        public Nullable<system.datetime> CreatedDate { get; set; }
        
    }
</system.datetime>







你的模特应该是根据数据库中的表格:






and your Model should be as per table in your Database:

public class Client
    {
              
        public string Mobile { get; set; }

        public string Password { get; set; }

        public string FirstName { get; set; }

        public Nullable<System.DateTime> CreatedDate { get; set; }

    }









和您的DataModel将与Table Mapping保持一致。要映射ViewModel和Model,您可以看到这篇文章


将此行添加到您的视图

@ Html.HiddenFor(model => model.ConfirmPassword)



和这对你控制器中的动作方法

Add this line to your view
@Html.HiddenFor(model => model.ConfirmPassword)

and this to your action method in controller
[HttpPost]
        public ActionResult Create(User user)
        {
            // TODO: Add insert logic here
            if (ModelState.IsValid)
            {
                user.ConfirmPassword = user.Password;
}
}


这篇关于在mvc中确认密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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