Is there simple way how to join two RouteValueDictionary values to pass parameters to Html.ActionLin

Posted by atagaew on Stack Overflow See other posts from Stack Overflow or by atagaew
Published on 2010-04-01T23:18:50Z Indexed on 2010/04/01 23:23 UTC
Read the original article Hit count: 293

Filed under:

Hi.

Look on my code that i created in a partial View:

            <% foreach (Customer customerInfo in Model.DataRows) {%>

            <tr>
                <td>
                    <%=Html.ActionLink(
                            customerInfo.FullName
                            , ((string)ViewData["ActionNameForSelectedCustomer"])
                            , JoinParameters(customerInfo.id, (RouteValueDictionary) ViewData["AdditionalSelectionParameters"])
                            , null)%>
                </td>
                <td>
                    <%=customerInfo.LegalGuardianName %>
                </td>
                <td>
                    <%=customerInfo.HomePhone %>
                </td>
                <td>
                    <%=customerInfo.CellPhone %>
                </td>
            </tr>
            <%}%>

Here I'm building simple table that showing customer's details.

As you may see, in each row, I'm trying to build a link that will redirect to another action.

That action requires customerId and some additional parameters.

Additional parameters are different for each page where this partial View is using. So, i decided to make Action methods to pass that additional parameters in the ViewData as RouteValueDictionary instance.

Now, on the view i have a problem, i need to pass customerId and that RouteValueDictionary together into Html.ActionLink method. That makes me to figure out some way of how to combine all that params into one object (either object or new RouteValueDictionary instance)

Because of the way the MVC does, i can't create create a method in the codebehind class (there is no codebihind in MVC) that will join that parameters.

So, i used ugly way - inserted inline code:

...script runat="server"...

    private RouteValueDictionary JoinParameters(int customerId, RouteValueDictionary defaultValues)
    {
        RouteValueDictionary routeValueDictionary = new RouteValueDictionary(defaultValues);

        routeValueDictionary.Add("customerId", customerId);

        return routeValueDictionary;                               
    }
...script...

This way is very ugly for me, because i hate to use inline code in the View part.

My question is - is there any better way of how i can mix parameters passed from the action (in ViewData, TempData, other...) and the parameter from the view when building action links.

May be i can build this link in other way ?

Thanks!

© Stack Overflow or respective owner

Related posts about asp.net-mvc