2011-06-09 64 views
0

我是新來的asp.net mvc。然而,這是我做了什麼:在asp.net中創建新的數據庫記錄時出錯mvc

在控制器中,

  public ActionResult Create() 
    { 


     return View(); 
    } 

    // 
    // POST: /Customerservice/Create 

    [HttpPost] 
    public ActionResult Create([Bind(Exclude="CustomerServiceMappingID")] Maping serviceToCreate) 
    { 
     if (!ModelState.IsValid) 

       return View(); 

      var dc = new ServicesDataContext(); 

      dc.Mapings.InsertOnSubmit(serviceToCreate); 
      dc.SubmitChanges(); 
      return RedirectToAction("Index","Home"); 
    } 

觀是這樣的:

 @Html.DropDownListFor(model => model.Status, new SelectList(new List<object> 
             {new {value="Active" , text="Active"}, 
             new {value="Pending", text="Pending" }, 
             new {value="Disabled", text="Disabled"}}, "value", "text", Model.Status)) 

有4個領域。但是,當我嘗試使用狀態,我得到一個異常說「對象引用未設置爲對象的實例」

回答

0

在GET操作,你需要通過模型視圖:

public ActionResult Create() 
{ 
    // the model could also be fetched from the DB given 
    // an unique ID passed as argument to this action 
    var model = new Maping(); 
    return View(model); 
} 
+0

沒有,它仍然不起作用 – Sayamima 2011-06-09 06:18:07

+0

@Nishanth,在這個改變之後你是否得到相同的異常?與之前一樣?我非常懷疑這一點。你很可能有其他問題。 – 2011-06-09 06:18:41

+0

我收到了同樣的錯誤... – Sayamima 2011-06-09 06:22:53