2010-06-10 59 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