4

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
  • 1
    Can't you just add the view model type as an argument on a Action method? public ActionResult ActionOnSecondController(MyViewModel model) {...}
    – mxmissile
    Commented Dec 3, 2009 at 19:31
  • Thought i could to, but i don't get any of the List<TBLRETURNABLEITEMS> items back. Could this be because i am using a partial class to display the List<TBLRETURNABLEITEMS> items? I wouldn't thinks so since both the view and the partial inherit from the model. Commented Dec 3, 2009 at 19:38

5 Answers 5

10

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);
    }
2
  • 1
    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. Commented Dec 4, 2009 at 14:16
  • If you're trying to pass a complex object you'll also need to serialize it. See this answer Commented Aug 31, 2022 at 21:53
3

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

3
  • 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. Commented Dec 3, 2009 at 19:22
  • OK. But how do you deal with concurrency issues? What happens if someone edits the data between your page views? Commented Dec 3, 2009 at 20:02
  • 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. Commented Dec 3, 2009 at 20:11
1

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.

-1

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.

1
  • 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. Commented Dec 3, 2009 at 20:17
-1

I think in MVC as well session is a handy option instead of firefighting with the return to action with Viewmodel.

1
  • Please explain your answer.
    – neophyte
    Commented Feb 3, 2017 at 2:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.