0

I am trying to send 2 variables to my controller with a Html.Actionlink, but everytime i do this, i get a NULL value, instead of the value i am trying to send.

This is my Html.Actionlink:

<ul>
@{ Spot selectedSpot = (Spot)Session["SelectedSpot"];}
@foreach (Spot spot in (List<Spot>)Session["AllSpots"])
{
    if (selectedSpot != null)
    {
        if (selectedSpot.Id == spot.Id)
        {
            <li>@Html.ActionLink(spot.ToString(), "SetSpot", "EmsUser", new { selectedSpot = spot, user = Model }, new { @class = "selected"})</li>
        }
        else
        {
            <li>@Html.ActionLink(spot.ToString(), "SetSpot", "EmsUser", new { selectedSpot = spot, user = Model }, null)</li>
        }
    }
    else
    {
        <li>@Html.ActionLink(spot.ToString(), "SetSpot", "EmsUser", new { selectedSpot = spot, user = Model }, null)</li>
    }
}
</ul>

This is my controller:

public ActionResult SetSpot(Spot selectedSpot, User user)
    {
        Session["SelectedSpot"] = selectedSpot;
        user.SetId(0);
        return View("MakeReservation", user);
    }

EDIT:

This is what the actionlink expects from me. "EmsUser" is the controller name and "SetSpot" is the action name enter image description here

Svenmarim
  • 3,633
  • 5
  • 24
  • 56
  • If "spot" is of type "Spot", why are you converting it to string in action link? – Saket Kumar Jun 01 '17 at 13:31
  • "spot" is indeed of type "Spot", and i use "spot.ToString()" to show all the fields of the class "spot" into the list – Svenmarim Jun 01 '17 at 13:35
  • My bad. Let me check. – Saket Kumar Jun 01 '17 at 13:51
  • Your `linkText` is `spot.ToString()` is that what you want? As I see `spot` is also your property getting passed to action method. – User3250 Jun 01 '17 at 13:55
  • @User3250, Yes, i want to display the "spot.ToString()" method, and after someone clicks on the line, i want to send that specific spot to the controller – Svenmarim Jun 01 '17 at 13:58
  • 1
    You shouldn't actually use ActionLink to post model to controller. Check [here](https://stackoverflow.com/a/21529498/4868839) and [here](https://stackoverflow.com/a/11600966/4868839) – User3250 Jun 01 '17 at 14:08

1 Answers1

2

I figured out what the issue is. The route arguements specified in the ActionLink are posted as query string parameters to controller action. You can't add instance type in the route arguements. If you wish to pass the objects, you will have to do some work arounds. Please see below my suggestions:

Method 1: Pass all the fields of the class individually in the route arguments. For e.g. lets say the classes are-

public class Model1
{
    public int MyProperty { get; set; }
    public string MyProperty1 { get; set; }
}

public class Model2
{
    public int MyProperty2 { get; set; }
    public string MyProperty3 { get; set; }
}

Your ActionLink should be:

@Html.ActionLink("Text", "SetSpot", "EmsUser",
new {
    MyProperty = 1,
    MyProperty1 = "Some Text",
    MyProperty2 = 2,
    MyProperty3 = "Some Text"
}, null)

Method 2:

Use ajax call as shown in this link

Saket Kumar
  • 4,363
  • 4
  • 32
  • 55
  • 1
    The key is that complex objects need to be in the request body, which requires using a request method like POST, rather than GET. Especially, with an object like `User`, which likely has very sensitive information on it, you don't want this stuff going into the actual URL, which is what will always happen with GET, AJAX or not. – Chris Pratt Jun 01 '17 at 14:08
  • @ChrisPratt: Totally agree. I guess ajax post can be used in that case. – Saket Kumar Jun 01 '17 at 14:10
  • I am going to give it an try now :) – Svenmarim Jun 01 '17 at 14:17
  • I tried your method, but now i only get the "selectedSpot" values in my controller and is the "user" this NULL. (My Model is of type "User" class) Here you can see my code i tried: https://pastebin.com/BX7neEWG – Svenmarim Jun 01 '17 at 14:38
  • I don't see a reason why user parameter should be null. I have verified the above code myself. Please check if "Model" has values. And could you also post your models (User and Spot)? – Saket Kumar Jun 02 '17 at 05:57