2014-02-25 137 views
0

我試圖讓編輯viewmodel工作。在[httpget]上,它沒有問題,但在[httppost]上,它不會保存/更新到數據庫權限。它將我發送給我的http錯誤404.0 - 未找到。使用編輯動作編輯ViewModel錯誤

這裏是我的代碼:

編輯動作在我的節日控制器:

[HttpGet] 
    public ActionResult Edit2(int? id) 
    { 
     //Festival fest = db.Festivals.Include("County").Include("FestivalType").Include("Towns").Where(x => x.FestivalId == id).Single(); 
     Festival fest = db.Festivals.Where(x => x.FestivalId == id).FirstOrDefault(); 

     FestivalVM festival = new FestivalVM { SelectedCounty = fest.FestivalCounty.ID, endDate = fest.EndDate, FestivalName = fest.FestivalName, SelectedFestivalType = fest.FType.ID, startDate = fest.StartDate, SelectedTown = fest.FestivalTown.ID }; 
     if (fest != null) 
     { 

      festival.County = db.Counties.ToDictionary(p => p.ID, q => q.Name); 
      festival.FestivalType = db.FestivalTypes.ToDictionary(p => p.ID, q => q.FType); 
      festival.FestivalType.Add(-1, "----- Add New Festival Type -----"); 
      festival.Towns = db.Towns.ToDictionary(p => p.ID, q => q.Name); 

      festival.startDate = (from f in db.Festivals where fest.FestivalId == id select f.StartDate).FirstOrDefault(); 

      festival.endDate = (from f in db.Festivals where fest.FestivalId == id select f.EndDate).FirstOrDefault(); 

      return View(festival); 
     } 
     return HttpNotFound(); 
    } 

    [HttpPost] 
    public ActionResult Edit2(FestivalVM model) 
    { 
     if (ModelState.IsValid != true) 
     { 
      if (model.SelectedFestivalType != -1) 
      { 
       Festival f = new Festival(); 
       //db.save stuff from create. 
       f.EndDate = model.endDate.Date; 
       f.FestivalCounty = db.Counties.Where(p => p.ID == model.SelectedCounty).Single(); 
       f.FestivalName = model.FestivalName; 
       f.FType = db.FestivalTypes.Where(p => p.ID == model.SelectedFestivalType).Single(); 
       f.StartDate = model.startDate.Date; 

       f.FestivalTown = db.Towns.Where(p => p.ID == model.SelectedTown).Single(); 
       f.UserID = WebSecurity.CurrentUserId; 


       //db.Entry(model).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Details", "Festival", new { id = f.FestivalId }); 
       //String test = "test3"; 
      } 
      ModelState.AddModelError("", "No Festival Type Picked"); 
     } 
     model.County = db.Counties.ToDictionary(p => p.ID, q => q.Name); 
     model.FestivalType = db.FestivalTypes.ToDictionary(p => p.ID, q => q.FType); 
     model.FestivalType.Add(-1, "----- Add New -----"); 
     model.Towns = db.Towns.ToDictionary(p => p.ID, q => q.Name); 
     model.startDate = DateTime.Now; 
     model.endDate = DateTime.Now; 

     return View(model); 
    } 

,編輯,查看

@model MyFestival.Models.FestivalVM 
@{ 
    ViewBag.Title = "Edit Your Festival"; 
    Layout = "~/Views/Shared/Festival.cshtml"; 
} 

<h2>Edit Your Festival</h2> 
@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <hr /> 
    @Html.ValidationSummary(true) 


    <div class="form-group"> 
     @Html.LabelFor(model => model.FestivalName, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(model => model.FestivalName, new{@class = "form-control", @style="width:250px" }) 
      @Html.ValidationMessageFor(model => model.FestivalName, null, new {@style="color:red;"}) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.startDate, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.startDate, new { @class = "form-control", @style = "width:250px" }) 
      <input class="form-control datepicker" style="width:250px" name="startDate" placeholder="Please pick date..."/> 
      @Html.ValidationMessageFor(model => model.startDate, null, new { @style = "color:red;" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.endDate, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.endDate, new { @class = "form-control", @style = "width:250px" }) 
      <!--<input class="form-control datepicker" style="width:250px" name="endDate" placeholder="Please pick date..."/>--> 
      @Html.ValidationMessageFor(model => model.endDate, null, new { @style = "color:red;" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Towns, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(p=> p.SelectedTown ,Model.Towns.Select(p=> new SelectListItem(){Text=p.Value.ToString(), Value=p.Key.ToString(),Selected=false}), new{@class = "form-control", @style="width:250px" }) 
      @Html.ValidationMessageFor(model => model.Towns, null, new {@style="color:red;"}) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.County, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(p=> p.SelectedCounty, Model.County.Select(p=> new SelectListItem(){Text=p.Value.ToString(),Value=p.Key.ToString(), Selected=false}), new{@class = "form-control", @style="width:250px" }) 
      @Html.ValidationMessageFor(model => model.County, null, new {@style="color:red;"}) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.FestivalType, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(p=> p.SelectedFestivalType, Model.FestivalType.Select(p=> new SelectListItem(){Text=p.Value.ToString(),Value=p.Key.ToString(), Selected=false}), new{@class = "form-control", @style="width:250px"}) 
      @Html.ValidationMessageFor(model => model.FestivalType, null, new {@style="color:red;"}) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Save" class="btn btn-info" /> 

      @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-danger" }) 
     </div> 
    </div> 
    <br /> 
</div> 
} 

@Html.Partial("CreateFestivalType", new MyFestival.Models.FestivalTypeVM()) 

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

    <script type="text/javascript"> 
     $(document).ready(function() { // will trigger when the document is ready 
      $('.datepicker').datepicker({ 
       format: 'dd-mm-yyyy', 
       currentText: 'Now' 
      }); //Initialise any date pickers 
     }); 
</script> 

    <script> 
     $(document).ready(function() { 
      $('#SelectedFestivalType').change(function() { 
       if ($(this).find(":selected").val() == -1) { 
        $('#myModal').modal('show'); 
        $('.focus :input:first').focus(); 
       } 
      }); 
     }); 
</script> 
<script type="text/javascript"> 
    function ajaxResponse(data) { 
     alert("This Worked and the Data ID is: " + data.FestivalTypeID); 
     var newOption = "<option value='" + data.FestivalTypeID + "'>" + data.Name + "</option>"; 
     $('#SelectedFestivalType').append(newOption); 
     $('#myModal').modal('hide'); 

     $("#SelectedFestivalType option[value='" + data.FestivalTypeID + "']").attr("selected", "selected"); 
    }; 
</script> 
}  
+0

讓我們來看看您的視圖 –

+0

@ TMcKeown它剛剛添加 – PatrickMelia

+0

控制器/視圖模型不匹配,請檢查我的答案。 –

回答

0

要保存更改調用db.SaveChanges前加入這一行() :

db.Festival.Add(f); 

您並未添加到上下文中。

+0

但爲什麼'db.Festival.Add(f);'?這創造了一個節日。它應該不是'db.Entry(f).State = EntityState.Modified;'? 即使我以爲我嘗試過使用EntityState.Modified,但我得到這個錯誤:**存儲更新,插入或刪除語句影響了意外數量的行(0)。自實體加載後,實體可能已被修改或刪除。刷新ObjectStateManager條目。** 我會.. – PatrickMelia

+0

我的觀點是你的節日對象沒有「附加」到dbContext。 –