开发者

mvc action link error message

What is wrong with this statement?

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


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
) %>


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜