13

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

0

5 Answers 5

8

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

Html.BeginForm("SaveAll", "Category", FormMethod.Post);

If you are in a controller then you can use.

TempData["Model"] = Model;
RedirectToAction("SaveAll", "Category");
Sign up to request clarification or add additional context in comments.

8 Comments

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.
Not this... I need if I'm already inside an action and to make a post to another action in other controller...
@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.
@AndreMiranda: You can pass your model using TempData.
TempData - I did not about that. Defiantly a nice thing to use. great answer +1
|
6
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.

Comments

5

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.

2 Comments

Repository is not my goal here... sorry
+1 for saying "refactor," for which I took an entire paragraph.
2

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.

2 Comments

Agreed, the BLL is the place for this.
If you're unit testing your controllers then your controllers are doing too much.
1

Put the following code in your Product controller:

return RedirectToAction("SaveAll", "Category")

Here, "SaveAll" is an Action Name and "Category" is Controller Name. The user will then be redirected to the SaveAll action (i.e., the method will be called).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.