Vikram
Vikram

Reputation: 181

Handling Http Post and Get in the same view - ASP.Net MVC Razor

My view has 2 label 2 text-boxes and a button.

This is my controller :

public ActionResult Create()
{
    // Custom model that holds values
    // this is to set the default values to the text boxes

    return View(model);
}

[HttpPost]
public ActionResult Create(CustomModel viewModel )
{
    try
    {
        // TODO: Add insert logic here

        // The  button should trigger this method to   perform  update  

        return RedirectToAction("Create");
    }
    catch
    {
        return View();
    }
}

When I run the program it automatically goes to my model which contains no value and throws a null pointer exception. Any ideas on how to preserve the model state. ?

Update :

I am using classic ADO.Net Model not the entity framework.

The http get and the post method follows a different logic. The Model with values are returned using the get method and the state of this model needs to be preserved for updating the corresponding records to the database using the post method.

But, when I use the post method, the compiler automatically routes to my model class looking for a parameter less constructor. I believe it is creating a new instance of my model class.

Upvotes: 0

Views: 2998

Answers (4)

Rajeshkumar Kandhasamy
Rajeshkumar Kandhasamy

Reputation: 6461

You have to create new instance in your Action Result method.

public ActionResult Create()
{
    // Assuming - First Time this ActioResult will be called.
    // After your other operations

    CustomModel model = new CustomModel();

    // Fill if any Data is needed
    return View(model);

    // OR - return new instance model here
    return View(new CustomModel());
}

Upvotes: 0

chconger
chconger

Reputation: 107

Your "Get" method is returning a model that isn't present in your code(from what you have shown here). Below is a way to have your Action accept GET & POST.

[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
public ActionResult Create(CustomModel viewModel)
{
    try
    {
        // TODO: Add insert logic here

        // The  button should trigger this method to   perform  update  

        return RedirectToAction("Create");
    }
    catch
    {
        return View();
    }
}

Upvotes: 0

Neel
Neel

Reputation: 11721

Well you can put validation in your view and then use ModelState.IsValid property as shown in below code :-

[HttpPost]
    public ActionResult Create(CustomModel viewModel )
    {
        if (ModelState.IsValid)
                {
                  ////Insert logic here
                  return RedirectToAction("Create");
                }

         return View(viewModel);
    }

Upvotes: 0

rmorrin
rmorrin

Reputation: 2485

Not entirely sure I follow, but you can return the same view from your POST action, passing in the model. This will preserve the model data.

[HttpPost]
public ActionResult Create(CustomModel viewModel )
{
    try
    {
        // TODO: Add insert logic here

        // The  button should trigger this method to   perform  update

        // Return "Create" view, with the posted model
        return View(model);
    }
    catch
    {
        // Do something useful to handle exceptions here.

        // Maybe add meaningful message to ViewData to inform user insert has failed?
        return View(model);
    }
}

Upvotes: 1

Related Questions