2

I am trying to pass multiple parameters to an action in my controller by doing this:

@Html.ActionLink("Set", "Item", "Index", new { model = Model, product = p }, null)

My action method looks like this:

public ActionResult Item(Pro model, Pro pro)
{
   ...
}

The problem is that the model and productToBuy variables in the action method are all null when the method is called. How come?

Steffan Pallesen
  • 140
  • 1
  • 15
  • 5
    You can't pass complex objects as this answer suggests http://stackoverflow.com/a/4197843/168097 – Alexandros B Dec 22 '12 at 17:49
  • The post @Circadian references does a great job of explaining that you __could__ serialize the complex object and pass it your controller, but also that it would be a BAD idea. IMHO -- There is a lot of potential for subtle confusion in this area. Consider the typical Edit / Edit-Save scenario which feels like our model object goes from the server to the client AND BACK AGAIN, as an object. But this is just a trick that the default ModelBinding is pulling off on our behalf. – David Tansey Dec 22 '12 at 19:25

1 Answers1

3

You cannot send complex objects as route parameters.B'cos it's converted into query string when passing to the action methods.So always need to use primitive data types.

It should be looks like below (sample)

@Html.ActionLink("Return to Incentives", "provider", new { action = "index", controller = "incentives" , providerKey = Model.Key }, new { @class = "actionButton" })

Your route table should be looks like below.Consist of primitive data types.

 routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

Solution 1

You can send a Id of the model as a parameter with ActionLink and then get the necessary object from the database for further processing inside the controller's action method.

Solution 2

You can use TempData for send objects from one Action Method to another. Simply it's share data between controller actions.You should only use it during the current and the subsequent requests only.

As an example

Model

public class CreditCardInfo
{
    public string CardNumber { get; set; }
    public int ExpiryMonth { get; set; }
 }

Action Methods

[HttpPost]
public ActionResult CreateOwnerCreditCardPayments(CreditCard cc,FormCollection frm)
  {
        var creditCardInfo = new CreditCardInfo();
        creditCardInfo.CardNumber = cc.Number;
        creditCardInfo.ExpiryMonth = cc.ExpMonth;
             
    //persist data for next request
    TempData["CreditCardInfo"] = creditCardInfo;
    return RedirectToAction("CreditCardPayment", new { providerKey = frm["providerKey"]});
  }


 [HttpGet]
 public ActionResult CreditCardPayment(string providerKey)
  {
     if (TempData["CreditCardInfo"] != null)
        {
         var creditCardInfo = TempData["CreditCardInfo"] as CreditCardInfo;
        }
      
      return View();
          
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sampath
  • 63,341
  • 64
  • 307
  • 441