新手问:我已将现有数据绘制到编辑屏幕..但无法让它更新。我可以创建并列出(在其他代码中),但在更新时做错了。我相信我在控制器中做错了什么。将按钮操作保存回编辑屏幕而不做任何更改。
控制器代码:
public class CompeditorController : Controller
{
private readonly BodyBuilderDB _db;
public CompeditorController(BodyBuilderDB db)
{
_db = db;
}
[HttpGet]
public ActionResult Edit(int CompeditorId)
{
var model = _db.Compeditors.Single(d => d.CompeditorId == CompeditorId);
return View(model);
}
[HttpPost]
public ActionResult Change(EditCompViewModel viewModel)
{
var compeditor = new Compeditor();
var bodybuilderDB = _db.Compeditors;
{
if (ModelState.IsValid)
compeditor.CompeditorId = viewModel.CompeditorId;
compeditor.FirstName = viewModel.FirstName;
compeditor.MiddleInt = viewModel.MiddleInt;
compeditor.LastName = viewModel.LastName;
compeditor.StreetAddress = viewModel.StreetAddress;
compeditor.City = viewModel.City;
compeditor.State = viewModel.State;
compeditor.PostalCode = viewModel.PostalCode;
compeditor.HomePhone = viewModel.HomePhone;
compeditor.CellPhone = viewModel.CellPhone;
compeditor.Height = viewModel.Height;
compeditor.Weight = viewModel.Weight;
compeditor.EmailAddress = viewModel.EmailAddress;
_db.Entry(bodybuilderDB).CurrentValues.SetValues(compeditor);
_db.SaveChanges();
}
结束代码:
<div class="editor-label">
@Html.LabelFor(model => model.Weight)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Weight)
@Html.ValidationMessageFor(model => model.Weight)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
型号:
using System;
using eManager.Core;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace eManager.Web2.Models
{
public class EditCompViewModel
{
[Key]
public int CompeditorId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string MiddleInt { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string EmailAddress { get; set; }
public string HomePhone { get; set; }
public string CellPhone { get; set; }
[Required]
public int Height { get; set; }
[Required]
public int Weight { get; set; }
}
}
答案 0 :(得分:1)
在保存更改
之前添加此项_db.Entry(bodybuilderDB).State = EntityState.Modified;
删除
var compeditor = new Compeditor();
compeditor.CompeditorId = viewModel.CompeditorId;
compeditor.FirstName = viewModel.FirstName;
compeditor.MiddleInt = viewModel.MiddleInt;
compeditor.LastName = viewModel.LastName;
compeditor.StreetAddress = viewModel.StreetAddress;
compeditor.City = viewModel.City;
compeditor.State = viewModel.State;
compeditor.PostalCode = viewModel.PostalCode;
compeditor.HomePhone = viewModel.HomePhone;
compeditor.CellPhone = viewModel.CellPhone;
compeditor.Height = viewModel.Height;
compeditor.Weight = viewModel.Weight;
compeditor.EmailAddress = viewModel.EmailAddress;
并改变
_db.Entry(bodybuilderDB).CurrentValues.SetValues(compeditor);
到
_db.Entry(bodybuilderDB).CurrentValues.SetValues(viewModel);