在asp.net mvc中创建新数据库记录时出错

时间:2011-06-09 06:08:28

标签: asp.net-mvc-3

我是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个字段。但是,当我尝试使用Status时,我得到一个异常,说“对象引用没有设置为对象的实例”

1 个答案:

答案 0 :(得分: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);
}