Error while creating new database record in asp.net mvc
I'm new to asp.net mvc. However, this is what I've done:
In the controller,
public ActionResult Create()
{
retu开发者_如何学编程rn 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");
}
The View goes like this:
@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))
There are 4 fields. However, when I try to use Status , I get an exception saying " Object Reference not set to an instance of object"
In the GET action you need to pass the model to the view:
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);
}
精彩评论