1

I have this method in my controller called "DirectorySearchController"

public ActionResult PersonDetails(FoundPerson person) //for some reason person is null here
{
    DirectoryViewModel viewModel = new DirectoryViewModel();
    viewModel.person = person;
    return View(viewModel);
}

When I pass some parameters to it from the view using Html.Actionlink it returns a null value

<ul data-role="listview">
    @if (ViewBag.Message == "NO RESULTS FOUND")
    {
        <li>@ViewBag.Message</li>
    }
    else
    {
            foreach (var employee in Model)
            {
                <li>
                    <div class="ui-grid-b">
                        <div class="ui-block-a" style="width:20%; vertical-align:middle"><img src="@employee.pictureURL" width="40px" height="40px"/></div>
                        <div class="ui-block-b" style="width:80%; vertical-align:middle">@Html.ActionLink(employee.name, "PersonDetails", "DirectorySearch", new { person = employee}, null)</div>
                    </div>
                </li>
            }
    }


</ul>

But the funny thing is that when I pass the parameter without using the "new" keyword it passes the correct value. However, the problem is I need to pass multiple parameters so I need to use the "new" keyword.

<div class="ui-block-b" style="width:80%; vertical-align:middle">@Html.ActionLink(employee.name, "PersonDetails", "DirectorySearch", employee, null)</div>
1
  • I have exactly the same problem - if I specify the parameters the controller method needs, it complains about a null value but if I pass the object, it magically selects the correct object property. Crazy! Commented Nov 29, 2012 at 10:46

2 Answers 2

2

I think you are not using the correct overload of ActionLink. Try with either of these

The first one should mostly work according to your case:

<%=Html.ActionLink(employee.Name, "PersonalDetails", "DirectorySearch", new { person = employee }, null)%>

<%=Html.ActionLink(employee.Name, "PersonalDetails", new {person = employee})%>

Just a recommendation here. Ideally I'd use an Input submit to post the data to the Controller. You may either use a Model class OR FormCollection in the controller side to retrieve the values you entered in the View.

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

1 Comment

Thanks CodeMaid, I tried it but it's still not working with either one Two questions though: 1) Is there a difference between using <%= ... %> syntax vs. what I have above? 2) I don't have any forms on this view, I am passing in a list of objects to the view from another controller method. Can I still use your recommendation?
1

You should not pass model to the controller action like new { person = employee } in Html.ActionLink. Because what happens is, when you generate the action link the object you pass is converted into RouteValueDictionary which will be passed to the UrlHelper to append the route dictionary values to the link.

When you pass an anonymous object like in the first case the RouteValueDictionary stores a single parameter with name person and to set the value it sees that you have passed an object, it can't serialize the complete instance and set it to the single property so all it does is set the type name of the model Employee as the value to the person. So you will see the generated link as http://someserver/DirectorySearch/PersonDetails?person=Models.Employee

If you pass an instance like you did in the second case then it iterate all the properties and create key/value pairs, while the keys are the property names and the values are the property values, finally they all appended to the link as querystrings. In the case the generated url will be http://someserver/DirectorySearch/PersonDetails?Property1=Value1&Property2=Value2

Correct

@Html.ActionLink(employee.name, "PersonDetails", "DirectorySearch", employee, null)

Wrong

@Html.ActionLink(employee.name, "PersonDetails", "DirectorySearch", new {person = employee }, null)

You can use anonymous object to pass route parameters to the action link but at the time you should not pass reference types but built-in types like integer, string..

Ex.

@Html.ActionLink(employee.name, "PersonDetails", "DirectorySearch", 
new { Id = 23, Name = "Mark" }, null)

7 Comments

thanks Mark, what about passing multiple parameters in the correct manner above?
When you are passing anonymous object you are pasing parameters as key/value pairs similar like dictionary so you see only one querystring parameter with the name "person" but when you pass an instance then all their properties are converted into querystring paremeters and that's what needed for model binding.
thanks again, sorry to sound ignorant, but what about passing multiple reference types, for exmaple passing employee and Model in the original code?
I dont't understand why you want to pass multiple reference types for GET action normally you should go for POST. But still you can do through the first way new { person1 = employee1, person2 = employee2 }.
lol but that still results in my original conundrum with the controller recognizing person1 and person2 as null objects. I need to pass a single employee (person) and I need to pass the whole list of employees (Model), but that is where I'm having difficulty.
|

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.