2015-11-18 36 views
0

我之前遇到過編輯視圖出現問題的問題。這已經從此解決了post從我的編輯視圖中未反映在數據庫中的更新

現在我需要從我的MVC視圖進行更新。更新似乎沒有提交到數據庫,因爲更改不會反映在我進行更新時。下面是我更新的ActionResult:

[HttpGet] 
    public ActionResult Update() 
    { 
     EditGameVM model = new EditGameVM(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Update(EditGameVM model) 
    { 
     try { 
      game objGame = new game 
      { 
       gameID = model.gameID, 
       gameName = model.gameName, 
       description = model.description, 
       gameRule = model.gameRule, 
       gamePicture = model.gamePicture.ToString() 
      };    
      objBs.gameBs.Update(objGame); 
      TempData["Msg"] = "Created Successfully!"; 
      return RedirectToAction("Edit"); 
     } 
     catch (DbEntityValidationException dbEx) 
     { 
      ... 
     } 
    } 

,這裏是相應的視圖:

@model TeamBuildingCompetition.ViewModels.EditGameVM 

@{ 
    ViewBag.Title = "Edit"; 
    Layout = "~/Views/Shared/_Layout_new.cshtml"; 
} 
<script> 
    tinymce.init({ selector: '#description' }); 
    tinymce.init({ selector: '#gameRule' }); 
</script> 

@using (Html.BeginForm("Update", "Game", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken() 

    <section id="middle"> 
     <div class="container"> 
      <div class="form-horizontal"> 
       <div class="center"><h1>Edit Games </h1></div> 
       @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
       @Html.HiddenFor(model => model.gameID) 
       <div class="form-group"> 
        @Html.LabelFor(model => model.gameName, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-8"> 
         @Html.EditorFor(model => model.gameName, new { htmlAttributes = new { @class = "form-control" } }) 
         @Html.ValidationMessageFor(model => model.gameName, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="form-group"> 
        @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-8"> 
         @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } }) 
         @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="form-group"> 
        @Html.LabelFor(model => model.gameRule, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-8"> 
         @Html.EditorFor(model => model.gameRule, new { htmlAttributes = new { @class = "form-control" } }) 
         @Html.ValidationMessageFor(model => model.gameRule, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="form-group"> 
        @Html.LabelFor(model => model.gamePicture, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-8"> 
         @Html.EditorFor(model => model.gamePicture, new { htmlAttributes = new { @class = "form-control" }}) 
        </div> 
       </div> 

       <div class="form-group"> 
        <div class="col-md-offset-2 col-md-10"> 
         <input type="submit" value="Create" class="btn btn-default" /> 
        </div> 
       </div> 
      </div> 
     </div> 
    </section> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 
+0

什麼是錯誤? –

+1

你有什麼異常嗎?並把你的gameBs.Update(objGame);方法代碼。 –

+3

我沒有看到你在dbcontext上調用'SaveChanges()' – user1666620

回答

0

起初我有我的BLL的代碼,不包括在我的倉庫模式的更新方法保存方法。

命名空間BLL { 公共類GameDB { 私人TBCDBEntities分貝;

public GameDB() 
    { 
     db = new TBCDBEntities(); 
    } 

    public void Update(game g) 
    { 
     db.Entry(g).State = EntityState.Modified;    
    } 

    public void Save() 
    { 
     db.SaveChanges(); 
    } 

所以我把它像這樣:

public void Update(game g) 
    { 
     db.Entry(g).State = EntityState.Modified; 
     Save();   
    } 

    public void Save() 
    { 
     db.SaveChanges(); 
    } 

它就像一個魅力!謝謝https://stackoverflow.com/users/1666620/user1666620

相關問題