如何避免某些领域模型进行更新使用asp.net mvc的4 [英] How to avoid certain fields to be updated in model using asp.net mvc 4

查看:147
本文介绍了如何避免某些领域模型进行更新使用asp.net mvc的4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来asp.net MVC 4和EF。我创建它创建用户模型/更新用户信息。在code为模型如下:

  ....
公众诠释用户名{搞定;组; }[需要]
公共字符串用户名{获得;组; }[需要]
公共字符串密码{搞定;组; }[比较(密码)]
公共字符串VerifyPassword {搞定;组; }
.....
 某些其它字段而来到这里
......

用于创建/更新活动本相同的模型。这工作正常创建用户,但同时更新我不想用户更新他的身份证,用户名,所以我把它只读也不想显示用户密码/验证密码字段,所以我从视图中删除它。现在,在更新(编辑模式),我总是得到密码字段验证错误消息。所以我的问题是如何避免更新这些领域和其他领域做的更新。为了解决这个问题,我尝试创建视图模型,但它并不能帮助我。还增加了code在控制器绕过验证是这样的:

 公众的ActionResult编辑(用户用户)
{
   ModelState.Remove(密码);
   ModelState.Remove(VerifyPassword);
   ModelState.Clear();
   如果(ModelState.IsValid)
   {
     db.Entry(用户).STATE = EntityState.Modified;
     db.SaveChanges();
     返回RedirectToAction(「指数」);
   }
}

这给我ModelState.IsValid =真实的,但无法更新。请给我解决了这个问题。

在此先感谢..


解决方案

  1. 您可以手动设置您只更新字段并保存更改。


  2. 您可以重置首先从数据库中取的值。类似如下(请原谅任何错误)

      VAR userFromDb = db.Users.Where(U => u.UserID == user.UserID)。首先();
    user.password的= person.Password;
    user.VerifyPassword = person.VerifyPassword;db.Entry(人).CurrentValues​​.SetValues​​(用户);
    db.SaveChanges();


  3. 创建一个自定义模型绑定,只有更新相关的字段。


I am new to asp.net mvc 4 and EF. I am creating User Model which creates/updates user info. The code for model is as follows:

....
public int UserID { get; set; }

[Required]
public string UserName { get; set; }

[Required]
public string Password { get; set; }

[Compare("Password")]   
public string VerifyPassword { get; set; }
.....
 SOME OTHER FIELDS COMES HERE
......

This same model used for create/update activity. This works fine for create user but while updating i don't want user to update his Id, username so I keep it readonly also don't want to show user password/verify password fields so I removed it from view. Now while updating(Edit mode) I am always getting validation error message for password field. So my question is how do I avoid updating these fields and do update for other fields. To solve this problem I try to create viewmodel but it doesn't help me. Also added code for bypassing validation like this in controller:

public ActionResult Edit(User user)
{
   ModelState.Remove("Password");
   ModelState.Remove("VerifyPassword");
   ModelState.Clear();
   if (ModelState.IsValid)
   {
     db.Entry(user).State = EntityState.Modified;
     db.SaveChanges();
     return RedirectToAction("Index");
   }
}

This give me ModelState.IsValid = true but fails to update. Please give me solution to this problem.

Thanks in advance..

解决方案

  1. You could manually set the fields that you are updating only and save the changes.

  2. You could reset the values taken from your database first. Something like the following (please forgive any errors)

    var userFromDb = db.Users.Where(u => u.UserID == user.UserID).First();
    user.Password = person.Password;
    user.VerifyPassword = person.VerifyPassword;
    
    db.Entry(person).CurrentValues.SetValues(user);
    db.SaveChanges();
    

  3. Create a custom model binder that only updates the relevant fields.

这篇关于如何避免某些领域模型进行更新使用asp.net mvc的4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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