2014-02-25 2 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)에 영향을줍니다. 엔터티가로드 된 이후에 엔터티가 수정되거나 삭제되었을 수 있습니다. Refresh ObjectStateManager entries. ** 나는 ~ – PatrickMelia

+0

내 요점은 당신의 축제 개체가 dbContext에 "첨부"되지 않았다는 것입니다. –