Calidus
Calidus

Reputation: 1404

Proper way to POST data MVC

Warning: This is my first web app.

I have 4 models, views and controllers. Lets call them A, B, C, D(ex. ModelA, ControllerA, ViewA). They are all basic views with list scaffolding.

/ControllerA/Index

User starts at ViewA and Selects an the first item, which redirects the user to ViewB

/ControllerB/Function?Aid=1

ViewB shows another list based on Selection from ViewA. Then the user Selects again is is redirected to ViewC

/ControllerC/Function?Aid=1&Bid=2

ViewC shows another list based on Selections from ViewA and ViewB. Then the user Selects again is is redirected to ViewD.

/ControllerD/Function?Aid=1&Bid=2&Cid=3

ViewD shows another list based on Selections from ViewA, ViewB, and ViewC, Then the user Selects again.

At this point I would like to POST Aid, Bid, Cid, and Did and save them in my database. Ideally the user would click the link, the data would be posted and then the site would redirect the user back to the homepage. Should I create another model and controller to Handle the post? I thought about trying to do the POST from controllerD but that doesn't seem like the proper way to do this.

The msdn tutorials only show posting directly from a view with a strongly typed model. I kinda stuck and I would prefer not to make this a complete mess.

Edit for Code Controller

  public ActionResult myFunction(int Aid = 0, int Bid, int Cid)
        {

           //query D stuff here


            if (D == null)
            {
                return HttpNotFound();
            }
            return View(D.ToList());



        }

        [HttpPost]
        [InitializeSimpleMembership]
        public ActionResult CreateQuote(int Aid, int Bid, int Cid, int Did)
        {
            Quote myQuote = new Quote();
            myQuote.Customer_ID_FK = (int)Membership.GetUser().ProviderUserKey;
            myQuote.A_ID_FK = Aid;
            myQuote.B_ID_FK = Bid;
            myQuote.C_ID_FK = Cid;
            myQuote.D_ID_FK = Did;

            if (ModelState.IsValid)
            {
                db.Quotes.Add(myQuote);
                db.SaveChanges();
                db.Quotes.Max();
                int mymax = db.Quotes.Max(q => q.ID);


                return RedirectToAction();

            }


            return View(D.ToList());
        }
        [HttpPost]
        [InitializeSimpleMembership]
        public ActionResult CreateQuote(Quote myQuote)
        {
            myQuote.Customer_ID_FK = (int)Membership.GetUser().ProviderUserKey;
            if (ModelState.IsValid)
            {

                db.Quotes.Max();
                int mymax = db.Quotes.Max(q => q.ID);
                db.Quotes.Add(myQuote);
                db.SaveChanges();

                return RedirectToAction();

            }

            return View(D.ToList());
        }

Upvotes: 0

Views: 202

Answers (1)

Jedediah
Jedediah

Reputation: 1944

It usually makes sense to put your post handler in the controller it's related to. This isn't always the case, as sometimes it would make more sense to make a new controller to handle all posts related to a certain task. You should also understand the distinction between a method IN a controller, and a controller. A controller is just a class that inherits from System.Web.Mvc.Controller and can have methods just like any other class. A perfectly reasonable controller could look like this:

public class DController : Controller
{

    //GET /d
    public ActionResult Index()
    {
        //MyModel is a class that would contain the logic to display
        //the selections from A, B, and C
        var model = new MyModel();
        return View(model);
    }

    //POST /d/saveresults
    //We only want this method to accept POST
    [HttpPost]
    public ActionResult SaveResults(MyEntity data)
    {
        var model = new MyModel();
        model.SaveResultsToDatabase(data);

        return Redirect("/");
    }
}

The important thing in a controller is to keep logical processing to a minimum. There's nothing wrong with having an if statement here and there, but the majority of your logic should be handled by your model. A controller is there primarily to pass data between your views and models.

Upvotes: 1

Related Questions