2014-01-24 38 views
1

新手问:我有现有的数据绘画到编辑屏幕..但无法让它更新。我可以创建并列出(在其他代码中),但在更新上做错了什么。我相信我在控制器中做了错误的事情。将按钮操作保存到编辑屏幕中,无需更改。无法获得数据更新

控制器代码:

 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; } 

    } 
} 

回答

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); 
+0

谢谢!我现在会尝试。 –

+0

嗯,我得到了同样的结果。数据被拖入编辑屏幕,当我修改任何值时,屏幕将重新绘制原始数据。任何其他想法? –

+0

这里是我做的改变:compeditor.Weight = ewModel.Weight; compeditor.EmailAddress = viewModel.EmailAddress; _db.Entry(bodybuilderDB).CurrentValues.SetValues(compeditor); _db.Entry(bodybuilderDB).State = EntityState.Modified; _db.SaveChanges(); } –