views:

257

answers:

4

Hello Everyone, 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.

+2  A: 

NerdDinner Step 6: ViewData and ViewModel
http://nerddinnerbook.s3.amazonaws.com/Part6.htm

Robert Harvey
Thank you for your response, however i don't want to entirely reload the model on every controller action. Just the initial. Don't see anything in this link detailing that.
Billy Logan
OK. But how do you deal with concurrency issues? What happens if someone edits the data between your page views?
Robert Harvey
That's a good point. Guess it looks like i might be headed down the road i didn't want to have to go down and that is databasing and reloading on each controller action.
Billy Logan
+2  A: 

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);
    }
rrejc
What if user presses F5?
queen3
You were the first to answer correctly. TempData is an accurate solution for this issue. After a little more research however, probably not a good idea for my situation due to db concurrency and our site sitting on load balanced server.
Billy Logan
A: 

Getting a model (any model) from the view to the controller is pretty simple.

Others on here have covered that part where you take in a model object as a param.

what you seem to be missing is the binding of the list, which happens based on the names in your form elements in the view. basically,they have to look like C# list elements as strings. So, not knowing any of the properties of your list elements, something like:

<%= Html.TextBox("model.TBLRETURNABLEITEMS[0].ItemId") %>

Obviously,that's just a string, so you can construct it in a loop to do many rows of returnables.

Paul
given my code above how would you incorporate this. I am still having trouble submitting the model and getting any items to appear in TBLRETURNABLEITEMS. Although i may not go down this route for database concurrency issues this would still answer my initial question.
Billy Logan
+1  A: 

You can use session for this kind of problems. or you can use tempdata if the posts are sequential . and fetching each time from DB works if you run application in Intranet networks.

ali62b