asp.mvc 4 EF的ActionResult编辑与鉴于并非所有领域 [英] asp.mvc 4 EF ActionResult Edit with not all fields in view
问题描述
标准的产生code博克为应对编辑保存表单
[HttpPost]
公众的ActionResult编辑(用户用户)
{
如果(ModelState.IsValid)
{
db.Entry(用户).STATE = EntityState.Modified;
db.SaveChanges();
返回RedirectToAction(「指数」);
}
返回查看(用户);
}
在各个领域都在编辑视图present工作。
在我的模型我有一个(原因很明显)我并不想传递作为一种无形的表单字段密码字段。
其结果密码为空(这是可以预料的)。
我应该如何code来处理这种情况?
-
获取密码值,我应该从数据库中获取的价值?
如果我使用:用户U = db.User.Find(user.ID); EF与具有相同的键的对象已经存在于ObjectStateManager 问题 -
我怎么解决ModelState.IsValid,因为它是假的。
我已经找到了previous MVC版本的答案,但不知道什么是实现这一目标的最优雅/有效的方式?
@freeride,乖戾我没有测试如果user.password的空,我希望它是零,如果它不是,那么它是由一个恶意用户注入。整个excersie是避免密码的用户操纵
删除密码字段(ModelState.Remove(密码);)不解决的ModelState验证问题。
现在仍然是问题,我该如何恢复密码的价值?以下作品:
[HttpPost]
公众的ActionResult编辑(用户用户)
{
用户V = db.Users.Find(user.ID);
ModelState.Remove(通行证);
user.password的= v.Password; //从数据库分配 如果(ModelState.IsValid)
{ //db.Entry(user).State = EntityState.Modified; //这将无法正常工作,改变了环境
db.Entry(ⅴ).CurrentValues.SetValues(用户); //我们现在需要使用此
db.SaveChanges();
返回RedirectToAction(「指数」);
}
返回查看(用户);
}
我不知道如果我不得不做一个额外的分贝之旅,以获取价值?我不得不改变code修复EF环境的变化,它的工作原理,但它是code吧?
在
如果(ModelState.IsValid)
添加
如果(string.IsNullOrEmpty(user.password的))
{
ModelState.Remove(密码);
}
我不知道如果我不得不做一个额外的分贝之旅,以获取价值?一世
不得不改变code修复EF环境的变化,它的工作原理,但
在code吧?
块引用>做它用不同的方式。不要使用一个实体对象作为模型。
创建其中只包含您需要更新数据,模型例如:公共类的usermodel
{
公众诠释标识{搞定;组; }
公共字符串名字{获得;组; }
公共字符串姓氏{搞定;组; }
}和现在:
[HttpPost]
公众的ActionResult编辑(的usermodel的usermodel)
{ 如果(ModelState.IsValid)
{
用户的用户= db.Users.Find(userModel.Id);
user.FirstName = userModel.FirstName;
user.Surname = userModel.Surname;
db.SaveChanges();
返回RedirectToAction(「指数」);
}
返回查看(的usermodel);
}我希望它能帮助。
The standard generated code bock for responding to the Edit save form
[HttpPost] public ActionResult Edit(User user) { if (ModelState.IsValid) { db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(user); }
works when all fields are present in the edit view.
In my model I have a password field that (for obvious reasons) I don't want to pass as an invisible form field. As a result password is null (which is to be expected). How should I code to handle this scenario?
get the password value, should I fetch the value from the database? if I use: User u = db.User.Find(user.ID); EF has issues with "an object with the same key already exists in the ObjectStateManager"
how do I tackle the ModelState.IsValid as it is false
I have found answers for previous mvc versions, but was wondering what is the most elegant / efficient way of achieving this?
@freeride, surly I don't have to test if user.Password is null, I expect it to be null and if it isn't then it was injected by a malicious user. The entire excersie was to avoid user manipulation of the password.
Removing the password field (ModelState.Remove("Password");) does solve the ModelState validation issue. Now remains the question, how do I restore the password value? Following works:
[HttpPost] public ActionResult Edit(user user) { user v = db.Users.Find(user.ID); ModelState.Remove("Pass"); user.Password = v.Password; // assign from db if (ModelState.IsValid) { //db.Entry(user).State = EntityState.Modified; // this won't work as the context changed db.Entry(v).CurrentValues.SetValues(user); // we need to use this now db.SaveChanges(); return RedirectToAction("Index"); } return View(user); }
I wonder if I'm obliged to do an extra db trip to fetch the value? I had to change the code to fix the EF context change, it works, but is the code right?
解决方案Before
if (ModelState.IsValid)
add
if (string.IsNullOrEmpty(user.Password)) { ModelState.Remove("Password"); }
I wonder if I'm obliged to do an extra db trip to fetch the value? I had to change the code to fix the EF context change, it works, but is the code right?
Do it in a different way. Don't use an entity object as your model. Create a model which contains only data you need to update,for example:
public class UserModel { public int Id { get; set; } public string FirstName { get; set; } public string Surname { get; set; } }
and now:
[HttpPost] public ActionResult Edit(UserModel userModel) { if (ModelState.IsValid) { User user = db.Users.Find(userModel.Id); user.FirstName = userModel.FirstName ; user.Surname = userModel.Surname; db.SaveChanges(); return RedirectToAction("Index"); } return View(userModel); }
I hope it helps.
这篇关于asp.mvc 4 EF的ActionResult编辑与鉴于并非所有领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!