tags:

views:

73

answers:

2

What is wrong with this statement?

<%= Html.ActionLink("Assign Users", new { Controller="Users", Action="Index", Query="Index", Page=2932 })%>

I`m having the following error:

Error 10 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments c:\Code\MvcUI\Views\Project\Index.aspx 17 22 MvcUI

Error 11 Argument '3': cannot convert from 'AnonymousType#1' to 'string' c:\Code\MvcUI\Views\Project\Index.aspx 17 54 MvcUI

+2  A: 

The compiler says it all. There's no such extension method. You need to pass the action name as a second parameter:

<%= Html.ActionLink(
    "Assign Users", 
    "Index",
    new 
    { 
        Controller = "Users", 
        Action = "Index", 
        Query = "Index", 
        Page = 2932 
    }
) %>

To avoid repeating the action name you could use this extension:

<%= Html.ActionLink(
    "Assign Users", 
    "Index",
    "Users",
    new 
    { 
        Query = "Index", 
        Page = 2932 
    },
    null
) %>

UPDATE:

If you have the default routes setup:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

This will generate a link of the type: http://localhost:2199/Users/Index/2932?Query=Index

<%= Html.ActionLink(
    "Assign Users", 
    "Index",
    "Users",
    new 
    { 
        Query = "Index", 
        Id = 2932 
    },
    null
) %>
Darin Dimitrov
The third parameter is supposed to be string and you're feeding it a collection. I don't think there exists such an overload.
Developer Art
@Developer Art, where did you see a collection?
Darin Dimitrov
I have to pass an id = 2932. How can I do that?
You rename the `Page` route parameter to `Id`.
Darin Dimitrov
This will depend on how your routes are setup.
Darin Dimitrov
A: 

From your previous comment

I`m getting the following: localhost:2593/Users?Query=Index&Page=2932 I want to have: Users\Index\id=2932

This is what you need:

<%= Html.ActionLink("Assign Users",
                    "Index",
                    "Users", 
                     new { id = 2932 }, 
                     null) %>

Your Index method may look like this:

public ActionResult Index()
{ 
   //..... 
}

Which means your url may read like so:

localhost:2593/Users/Index?id=2932

You could also make id a parameter in your action method. So your Index method definition may look like this in your controller:

public ActionResult Index(int id)
{ 
 //..... 
}

and your url would look like this

localhost:2593/Users/Index/2932
Nick Masao