2012-07-29 4 views
0
내가 MVC에서 데이터베이스에 정보를 보내고 내가 페이지를 리디렉션하고 새로 저장된 데이터를 출력보기를 호출

, 나는 NullReferenceException이 오류가 개체의 인스턴스. "MVC NullReferenceException이

분명히, 내 모델은 null입니다 :

public ActionResult Index() 
    { 
     var customer = db.Customer.ToList(); 
     return View(); 
    } 

에 의해 호출됩니다 :이 널 왜 이해가 안

 [HttpPost] 
    public ActionResult Create(CustomerModel customer) 
    { 
     db.Customer.Add(customer); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.Name) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.CustomerID) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Address) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | 
     @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
     @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
    </td> 
</tr> 
} 

이 내 컨트롤러입니다 ....

답변

3
public ActionResult Index() 
    { 
     var customer = db.Customer.ToList(); 
     return View(customer); 
    } 

customer 개체를 절대로 뷰에 전달하지 않습니다.

편집 : 예외가 발생하면 try/catch에서 db.SaveChanges();을 래핑하지 않아도 위험 할 수 있습니다.

+0

Perfect. 고맙습니다. – Subby

관련 문제