Arianule
Arianule

Reputation: 9043

passing in an additional parameter with the model to a View

Still learning Mvc so sorry if this question is a bit weird. I am passing the model in my Controller method and want to pass an additional parameter.

what I want to achieve is something like this but the parameter overload does not allow for the 'additional' parameter.

 @using (Html.BeginForm("SubmitCall", "Home", new { Id = Model.ProductDetail.ProductId }))

and then in the controller

public ActionResult SubmitCall(ViewModel model, int Id)
    {

        return RedirectToAction("DetailViewById", model, new {productId = Id});//not allowed
    }

Is there a better way of achieving this?

Kind regards

Upvotes: 0

Views: 128

Answers (3)

ljubomir
ljubomir

Reputation: 1525

If your only intention is to redirect the client to another URL then the above scenario is not the right way to achieve this.

RedirectToAction will send a HTTP 302 with the Location information generated from your route information (again, in your case this would be the URL to DetailViewById with productId as a parameter or part of the URL - depends on your route configuration).

If you need to persist the ViewModel that is submitted by the client and operate on it once the user is requesting your redirected action, then i would suggest to use TempData which is designed for between-request persistence of such data.

Upvotes: 2

Zabavsky
Zabavsky

Reputation: 13640

Create an anonymous object for parameters:

return RedirectToAction("DetailViewById",
    new {
            productId = Id,
            model.Property1,
            model.Property2,
            ...
        });

Upvotes: 1

Ash
Ash

Reputation: 2595

Usually, the ViewModel is a combination of what you have coming back from database (DTO) + extra bits and pieces you need TO and FROM View.

So in my opinion, you should add this extra property to your Viewmodel.

Upvotes: 1

Related Questions