本文介绍了在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

推荐答案

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,您可以看到


[HttpPost]
        public ActionResult Create(User user)
        {
            // TODO: Add insert logic here
            if (ModelState.IsValid)
            {
                user.ConfirmPassword = user.Password;
}
}


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

08-31 06:09