Asp.net mvc 无法在mvc中编辑用户信息
这是我的问题,所以我想只允许用户更改/编辑他们的密码和用户名。Asp.net mvc 无法在mvc中编辑用户信息,asp.net-mvc,model-view-controller,Asp.net Mvc,Model View Controller,这是我的问题,所以我想只允许用户更改/编辑他们的密码和用户名。 我最初的客户模型是这样的 public string IC { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Username{ get; set; } public string Password{ get; set; } 这是我为客户提供的虚拟机 public string
我最初的客户模型是这样的
public string IC { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username{ get; set; }
public string Password{ get; set; }
这是我为客户提供的虚拟机
public string Username{ get; set; }
public string Password{ get; set; }
这是我的编辑功能控制器
public ActionResult Edit([Bind(Include = "Username,Password")] CustomersVM customersVM )
{
if (ModelState.IsValid)
{
db.Entry(customersVM ).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customersVM );
}
view.cshtml
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.IC)
<div class="form-group">
@Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@HiddenFor(model=>model.IC)
@LabelFor(model=>model.Username,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Username,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Username,“,new{@class=“text danger”})
@LabelFor(model=>model.Password,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Password,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Password,“,new{@class=“text danger”})
所以这个虚拟机是为了让ModelState变得有效,它将直接进入数据库,但结果证明它会导致这种类型的错误
System.InvalidOperationException:“实体类型CustomerVM不是当前上下文模型的一部分。”
为了编辑/更新记录,您需要首先识别该记录。在您的案例中,您的
ViewModel
不是您的数据库所拥有的,而是您问题中的第一个模型。因此,您需要在保存之前将该viewModel映射到真实模型,或者获取现有记录,然后对其进行修改,然后在保存之前将其设置为modified
public ActionResult Edit([Bind(Include = "Username,Password")] CustomersVM customersVM )
{
if (ModelState.IsValid)
{
var existing = db.Customers.FirstOrDefault(x => x.Id == customersVM.Id);
if (existing != null)
{
existing.Username = customersVM.Username;
existing.Password = customerVM.Password;
db.Entry(existing ).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(customersVM );
}
这是一个想法。。。您确定数据库中有customersVM表吗?视图是什么样子的?@Dani否,我需要让模型状态正确返回true。所以VM要让返回的值为true。但是我如何让新编辑的值传递到database@christiandev我已使用视图adder编辑了问题。如果您确定使用的是
CustomerVm
,我会看到CharityId
的属性?