实体框架 - 更新记录显示注释错误

时间:2015-05-05 07:02:21

标签: c# asp.net-mvc entity-framework entity

这是表格的模型。

public class Master
    {        
        public int Id { get; set; }
        public string EmailId { get; set; }
        public string UniqueCode { get; set; }
....
        [Required]          
        public string Password { get; set; }
        [Required]
        [NotMapped]
        [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

    }

当我尝试使用以下代码更新Above记录时,我收到验证错误The password and confirmation password do not match.The confirmation password is required.

未映射确认密码属性。如何在更新记录时设置注释?  : -

 var data = ctx.tblMaster.Where(m => m.EmailId == emailId).Select(m => m).SingleOrDefault();
                    data.UniqueCode = UniqueCode;

                    ctx.tblMaster.Attach(data);
                    ctx.Entry(data).State = EntityState.Modified;
                    ctx.SaveChanges();

1 个答案:

答案 0 :(得分:0)

这可以简单地做到前端。发生的验证是基于html5的数据属性完成的。从输入元素中删除数据属性,验证将停止运行。

您可以有条件地从控制器中删除模型状态中的字段,如此

示例代码:

  if (Request.IsAuthenticated)
  {
...

// We don't need to validate user fields if user is logged in.
ModelState.Remove("CommenterName");
ModelState.Remove("Email");
 }
相关问题