ZVenue
ZVenue

Reputation: 5027

Pass values between two actions in a controller

MVC Controller Action 1 : Displays values on a view page.

MVC Controller Action 2: Triggered by action link on the same view page - Takes the displayed values on the view page (values calculated in action 1) as parameters and runs its own logic.

Both are part of the same controller. Right now I am able to display values via action 1 successfully. Now the user clicks on the action link that takes execution to the action 2 .. But I am not able to successfully pass these values back to the controller action 2.

Controller Action 1:

 public ActionResult CalculationsONE()
{       
    var CalcModel = new CalcViewModel();

    int var1 = //Value calculated here
    int var2 = //Calculation

    CalcModel.Var1= var1;
    CalcModel.Var2= var2;

    return View(CalcModel);
}

View : The values from above are displayed on the view page.. like this.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CalcModel>" %>

<table>
    <tr><td>First Value:</td><td><%=Model.Var1%></td></tr>
    <tr><td>Second Value:</td><td><%=Model.Var2%></td></tr>
</table>

<%=Html.ActionLink("StringTitle", "Action2", "Controller", new { var1 = Model.Var1,    

 var2=Model.Var2})%>

Now when the user clicks the action link, the values should be passed to Action 2 of controller. But thats not happening.. please help.

Action 2 code:

   public ActionResult Action2(int? var1, int? var2)
    {
       //Other logic/calculations.
    }

var1 and var2 are getting nulls from the view ..

Upvotes: 0

Views: 1410

Answers (2)

Travis J
Travis J

Reputation: 82267

Your naming convention is wrong I believe, try

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CalcView>" %>

Upvotes: 0

Tejs
Tejs

Reputation: 41236

It's not a good or bad practice really; I simply find using the HTML Helpers for really simple links is overkill. In your situation, you were probably ending up attaching those values in the anonymous object as attributes on your link, instead of in the url.

Upvotes: 2

Related Questions