C# 模型未在POST上填充
我创建了一个简单的类来直接测试这一点,在Edit POST操作中,我感觉如果属性的形式不正确,它将使用数据库中已经存在的值,而不是设置为null并覆盖任何值 这是从scaffold生成的,假设我注释掉密码和密码确认,因为我不想让他们编辑它,所以他们会被遗漏在POST值之外C# 模型未在POST上填充,c#,asp.net-mvc,entity-framework-6,C#,Asp.net Mvc,Entity Framework 6,我创建了一个简单的类来直接测试这一点,在Edit POST操作中,我感觉如果属性的形式不正确,它将使用数据库中已经存在的值,而不是设置为null并覆盖任何值 这是从scaffold生成的,假设我注释掉密码和密码确认,因为我不想让他们编辑它,所以他们会被遗漏在POST值之外 @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4&
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PasswordConfirmation, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PasswordConfirmation)
@Html.ValidationMessageFor(model => model.PasswordConfirmation)
</div>
</div>*@
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
在这里,我只想保存编辑过的值,任何未包含的值都使用原始值
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,Email,FirstName,LastName,Password,PasswordConfirmation")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
这只是一个例子,可以帮助我理解当对象不在表单中时如何填充对象,如果有人可以详细说明或指向资源,这些资源清楚地显示了当值不在POST请求中显式填充时如何填充模型,这将非常棒
我的印象是,如果一处房产的形式不符合它的要求
使用数据库中已存在的值
事实并非如此。表单(只是客户端浏览器中的一些HTML)如何知道数据库中的值
您有两种解决方案
1)在HttpGet方法中,将实体的所有属性发送到表单,但不向用户显示这些属性。保存在表单内的隐藏字段中,当您发布表单时,它将可用
public ActionResult Edit(int id)
{
var vm = new EditUserVM();
var user = GetUserFromYourDb(id);
vm.Name = user.Name
vm.Email= user.Email
//The below property you don't want user to edit in form. But still sending.
vm.Token= user.Token
return View(vm);
}
在你看来
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
<!-- Hidden field here -->
@Html.HiddenFor(s=>s.Token)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
在你看来
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
<!-- Hidden field here -->
@Html.HiddenFor(s=>s.Token)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
我的印象是,如果一处房产的形式不符合它的要求
使用数据库中已存在的值
事实并非如此。表单(只是客户端浏览器中的一些HTML)如何知道数据库中的值
您有两种解决方案
1)在HttpGet方法中,将实体的所有属性发送到表单,但不向用户显示这些属性。保存在表单内的隐藏字段中,当您发布表单时,它将可用
public ActionResult Edit(int id)
{
var vm = new EditUserVM();
var user = GetUserFromYourDb(id);
vm.Name = user.Name
vm.Email= user.Email
//The below property you don't want user to edit in form. But still sending.
vm.Token= user.Token
return View(vm);
}
在你看来
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
<!-- Hidden field here -->
@Html.HiddenFor(s=>s.Token)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
在你看来
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
<!-- Hidden field here -->
@Html.HiddenFor(s=>s.Token)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
@model EditUserVM
@using(Html.Beginform())
{
@Html.TextBoxfor(s=>s.Name)
@Html.TextBoxfor(s=>s.Email)
@Html.HiddenFor(s=>s.UserId)
<input type="submit" />
}
是的,当然。切勿将发布的对象直接保存到数据库。使用视图模型而不是绑定,然后将值映射到/从您的视图模型映射到实体。感谢您的详细响应,我对另一个模型感到有些奇怪,该模型是从想法形成的帖子之外的某个地方填充属性的,但为了清晰起见,从现在起我将切换到视图模型。是的,当然。切勿将发布的对象直接保存到数据库。使用视图模型而不是绑定,然后将值映射到/从您的视图模型映射到实体。感谢您的详细响应,我在另一个模型中遇到了一些奇怪的情况,该模型从想法形成的帖子之外的某个地方填充属性,但为了清晰起见,从现在起我将切换到视图模型。