Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I return an object from a method executed with RedirectToAction?
    text
    copied!<p>I have a controller with Get, Save and Create actions. These actions take as a parameter an item type. This is just a string specifying what type of item we are processing.</p> <p>There is a child controller for each of these item types, and each of these child controllers implement the child controller interface (IChildController) which has the Get, Save and Create methods.</p> <p>Each child controller is named based on its type (ItemTypeController). When I'm getting an item from my parent controller, I can do it like this:</p> <pre><code>Get(string itemType, int id) { return RedirectToAction("Get", itemType, new { id }); } </code></pre> <p>The Get method in the child controllers has an ActionResult return type (returns a Partival View) so this works without problem.</p> <p>However, when I'm saving an item, all children are created and then returned to the parent object and stored within it so that a save can be performed on the parent only (and NHibernate will take care of cascading records).</p> <p>How can I return objects from these methods without instantiating all the child controllers first. For example, I'd like to be able to call Save(itemType, item)</p> <pre><code>Save(string itemType, Item item) { return RedirectToAction("Save", itemType, new { item }); } </code></pre> <p>and then we will find the appropriate child controller (itemTypeController), execute the method Save(item), and then return the item (item) back to the parent controller.</p> <pre><code>Save(Parent parent) { IRepository&lt;Parent&gt; _parent; var item = new Parent(); item.Id = parent.Id; item.Children = parent.Children.AddAll(parent.Children.Select(Save(parent.ChildType)); // save each child and add it to the parent's child collection return _parent.Save(item); } Save(Child child) { IRepository&lt;Child&gt; _child; var item = new Child(); item.Id = child.Id; return _child.Save(item); } </code></pre> <p>What is the best solution to achieve this? Thanks in advance for any help.</p> <p><strong>EDIT</strong> </p> <p>How would I go about converting complex objects into ViewModels. For example, a Parent object that contains children of type Child.</p> <p>In the ParentTypeController we have:</p> <pre><code>ParentViewModel ToViewModel(Parent parent) { var model = new ParentViewModel(); model.Id = parent.Id; model.Children = parent.Children.Select(ToViewModel); //Use the ToViewModel method in the ChildTypeController return model; } </code></pre> <p>ChildTypeController:</p> <pre><code>ChildViewModel ToViewModel(Child child) { var model = new ChildViewModel(); model.Id = child.Id; return model; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload