开发者

Error while creating new database record in asp.net mvc

开发者 https://www.devze.com 2023-03-11 06:01 出处:网络
I\'m new to asp.net mvc. However, this is what I\'ve done: In the controller, public ActionResult Create()

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);
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消