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

6
  • If "spot" is of type "Spot", why are you converting it to string in action link? Commented Jun 1, 2017 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 Commented Jun 1, 2017 at 13:35
  • My bad. Let me check. Commented Jun 1, 2017 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. Commented Jun 1, 2017 at 13:55
  • 1
    You shouldn't actually use ActionLink to post model to controller. Check here and here Commented Jun 1, 2017 at 14:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@ChrisPratt: Totally agree. I guess ajax post can be used in that case.
I am going to give it an try now :)
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: pastebin.com/BX7neEWG
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)?

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.