views:

314

answers:

4

Hi!

Is is possible to do a post from an Action "Save" in a controller "Product" to an Action "SaveAll" in a controller "Category"??

And also passing a FormCollection as parameter

Thanks!!

+1  A: 

You can declare a form like this in your View and can specify whatever controller or Action you wish.

Html.BeginForm("Action", "Controller", FormMethod.Post);

If you are in a controller then you can use.

TempData["Model"] = Model;
RedirectToAction("Action", "Controller");
Amitabh
I think what the OP is trying to do is update his categories from the Product View, and he wants to keep it all DRY.
Robert Harvey
Not this... I need if I'm already inside an action and to make a post to another action in other controller...
AndreMiranda
@AndreMiranda, OK, but isn't the end goal to save your categories? Do you care how that happens as long as it happens, using best practices? What are you trying to accomplish? If you just need to call another controller method, that can be done easily enough, without creating a POST request to do it.
Robert Harvey
+` for RedirectToAction(). However, it's not clear if that will let you pass the model or form collection as it relies on having the browser execute/respond to an HTTP 302 error. Also, that may have negative consequences for unit testing.
David Lively
Sorry, but it I really need to make a POST from one action to another action... I just illustrate my question using "Products" and "Category". But, my really question is about the post
AndreMiranda
And also I want to pass a FormCollection
AndreMiranda
@AndreMiranda: You can pass your model using TempData.
Amitabh
+1  A: 

Since POST is a verb for an HTTP request, this only makes sense (as written) if the .Save() method initiates an HTTP loopback connection to the appropriate .SaveAll(), (like http://..../Category/SaveAll) route and passes the form collection as part of the request. This is silly and not recommended, since this will break your ability to unit test this controller.

If, however, you mean that you want to call .SaveAll() and return its rendered result back to the client, you could use .RenderAction() and pass the model or form collection received by .Save() as the parameter.

Or, on the server side, just instantiate the Category controller and call its .SaveAll() method, again passing the model received by .Save() as the parameter.

public ActionResult Save(MyModel m)
{
    Category cat = new Category();

    return cat.SaveAll(m);
}

However, you'll have to take the result from that call and make sure it's handled properly by the resulting view.

If this is what you're trying to do, it's worth noting that you should really have the code of the .SaveAll() method that performs the save separated into a dedicated business logic layer rather than living in the controller. All of this functionality should, in theory, be available for use in a different controller, or in a library that could be included in other applications.

David Lively
Agreed, the BLL is the place for this.
Robert Harvey
+3  A: 

I would either just update your categories in your repository from your Product controller Save method directly, or refactor the Save Categories functionality in its own method, and call that from both controller methods.

Robert Harvey
Repository is not my goal here... sorry
AndreMiranda
+1 for saying "refactor," for which I took an entire paragraph.
David Lively
+2  A: 
public class Product : Controller
{
    ...
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(FormCollection productValues)
    {
        ...
        RedirectToAction("SaveAll", "Category", new { formValues = productValues });
    }
    ...
}

public class Category : Controller
{
    ...
    public ActionResult SaveAll(FormCollection formValues)
    {
        ...
    }
}

The assumption is that you are executing the POST in the context of the Product.

Neil T.