在asp.net中创建新的数据库记录时出错mvc
问题描述:
我是新来的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个领域。但是,当我尝试使用状态,我得到一个异常说“对象引用未设置为对象的实例”
答
在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);
}
没有,它仍然不起作用 – Sayamima 2011-06-09 06:18:07
@Nishanth,在这个改变之后你是否得到相同的异常?与之前一样?我非常怀疑这一点。你很可能有其他问题。 – 2011-06-09 06:18:41
我收到了同样的错误... – Sayamima 2011-06-09 06:22:53