Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - How to pass model from view to controller

I am working with a model that needs to flow through a series of controllers and views manipulating it along the way(Only loading it on the first controller). Is there a way to persist the model from the view back down to a controller and so forth?

Here is my code.

Model:

    public class ROWModel
{
    #region Properties
    //Request
    public List<TBLRETURNABLEITEMS> TBLRETURNABLEITEMS { get; set; }
    //public List<ReturnReasons> ReturnReasons { get; set; }

    public int Order_No { get; set; }
    public string First_Name {get; set; }
    public string Last_Name {get; set; }
    public string Company { get; set; }
    public string Address_1 { get; set; }
    public string Address_2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Postal_Code { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string CustomerCode {get; set; }
    public string TerritoryCode {get; set; }


    //Post

    #endregion

    #region Constructor
    public ROWModel()
    { }
    #endregion
}

public class ReturnableItems : IComparable<ReturnableItems>
{
    private int _id;
    private decimal _ordered;
    private decimal _shipped;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public decimal Ordered
    {
        get { return _ordered; }
        set { _ordered = value; }
    }

    public decimal Shipped
    {
        get { return _shipped; }
        set { _shipped = value; }
    }

}

After populating the model and sending it to the view everything is displayed using the model as it should be. I think stick the model like so on the form tag:

<% using (Html.BeginForm("Items", "ROW", Model))

Here is the post Items Action of the ROW controller:

    [ActionName("Items"), AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Items(ROWModel model, FormCollection collection)

Problem is Model doesn't return the list of TBLRETURNABLEITEMS i populated it with initially. It keeps the other properties i populated but not the list. How do i maintain this model's data without having to reload it on every controller should i want to.

like image 223
Billy Logan Avatar asked Dec 23 '22 07:12

Billy Logan


1 Answers

I think that you can use TempData for that.

So something like this:

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult TestA()
    {
        MyModel model = new MyModel();
        model.Something = "Test";
        return View(model);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult TestA(MyModel model)
    {
        TempData["MyModel"] = model;
        return RedirectToAction("TestB");
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult TestB()
    {
        MyModel myModel = (MyModel)TempData["MyModel"];
        return View(myModel);
    }
like image 174
rrejc Avatar answered Dec 24 '22 21:12

rrejc