2010-06-10 44 views
0

我已經創建了兩個創建動作..一個調用創建視圖,另一個使用httppost處理創建視圖。MVC空模型問題

當我調用創建視圖時,它會正確發佈,下拉菜單和所有。 問題是,當我填寫創建表單並單擊提交按鈕時,出現錯誤;

未將對象引用設置爲對象的實例。

我的第一個想法是,我將一個空模型傳遞給httppost創建動作。 如何檢查我是否將空模型傳遞給httppost創建動作?

謝謝

回答

0

你究竟在哪裏得到這個異常?它是在控制器操作中還是在渲染視圖時?通常的模式是:

public ActionResult New() 
{ 
    // as you will be creating a new entity you don't need to pass 
    // any model here unless your view depends on some property of the model 
    return View(); 
} 

[HttpPost] 
public ActionResult Create(SomeModel model) 
{ 
    // The model parameter here will be automatically instantiated 
    // by the default model binder and its properties will be 
    // initialized to the values entered by the user in the view 
    if (ModelState.IsValid) 
    { 
     // save the entity 
     Repository.Save(model); 
     // redirect back to the index action 
     return RedirectToAction("Index"); 
    } 
    // validation failed => pass the model to the view in order to 
    // preserve values and show validation errors 
    return View("New", model); 
} 
+0

看來我的ModelState是invalid..i在形式約爲5場,但只有2拿起.. 我也意識到,驗證消息不fire..any關於這些的想法? – femi 2010-06-12 09:37:48

+1

你可以發佈你的代碼嗎? – 2011-08-23 09:46:48