2013-03-06 1 views
0

나는 책을 읽고있다. Prof ASP.NET MVC3 Framework - Freeman Sandersan 간단한 관리 인스턴트 메신저를 만드는 방법이 나와있다. 문제는 저장하지 않는 제품의 변경 사항을 저장하려고 할 때입니다.MVC EditorForModel Freeman

편집보기 :

@using (Html.BeginForm()) { 
@Html.EditorForModel() 
<input type="submit" value="Save" />} 

편집 방법 :

[HttpPost] 
public ActionResult Edit(Product product) 
{ 
    if (ModelState.IsValid) 
    { 
     repository.SaveProduct(product); 
     TempData["message"] = string.Format("{0} has been saved", product.Name); 
     return RedirectToAction("Index"); 
    } 
    // there is something wrong with the data values 
    return View(product); 
} 

저장 제품 방법 : 편집

private EFDbContext context=new EFDbContext(); 
public void SaveProduct(Product product) 
{ 
    if (product.ProductID == 0) 
    { 
     context.Products.Add(product); 
    } 
    context.SaveChanges(); 
} 

결과 : I 제품의 이름을 변경하려면 "Kayakkkkkkkkkk" . 임시 메시지에 저장이 완료되었지만 제품 이름은 여전히 ​​"카약"이라고 표시됩니다. Results of editing

답변

0

해결되었습니다.

public void SaveProduct(Product product) 
{ 
    if (product.ProductID == 0) 
    { 
     context.Products.Add(product); 
    } 
    else 
    { 
     context.Products.Attach(product); 
     context.Entry(product).State = EntityState.Modified; 
    } 

    context.SaveChanges(); 
} 
관련 문제