views:

137

answers:

1

hello, i have created two create actions..one to call the create view and the other to process the create view using httppost.

when i call the create view, it gets published correctly , dropdowns and all. the problem is that when i fill out the create form and click on the submit button, i get an error;

Object reference not set to an instance of an object.

My first thoughts are that i am passing a null model to the httppost create action.. How can i check to see if i am passing in a null model to the httppost create action?

thanks

A: 

Where exactly you are getting this exception? Is it in the controller action or while rendering the view? The usual paradigm is the following:

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);
}
Darin Dimitrov
seems my modelstate is invalid..i have about 5 fields in the form but only 2 are picked up..i also realized that the validation messages dont fire..any ideas on these?
femi