Issue with ActionLink in MVC
How can I just write
Response.Write("<a href='/someurl'>Some Text</a>");
Instead of
Response.Write(Html.ActionLink(...));
This reason I am after this is because I am writing a Pagination ViewUserControl. In the control I want it to show page numbers, next and previous pages etc.
When I use the following code
Response.Write(Html.ActionLink(page.ToString(), HttpContext.Current.Request.RawUrl, new { page = page }));
The link is written out as http://localhost:61293/customers/customers/System.Web.Mvc.UrlParameter/page2
which is obviously incorrect.
HttpContext.Current.Request.RawUrl == "/customers/"
in this example. I would have expected that the resultant Url was then /customers/page2
as opposed to what is actually written out.
My routes are set up like so:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/page{page}", // URL with parameters
new
{
controller = "Home",
开发者_JS百科 action = "Index",
id = UrlParameter.Optional,
page = UrlParameter.Optional
} // Parameter defaults
);
Can anyone shed some light?
Based on your update, you shouldn't be using HttpContext.Current.Request.RawUrl
as the action of your ActionLink
.
http://msdn.microsoft.com/en-us/library/dd505040(v=VS.90).aspx
public static string ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
object routeValues
)
As you can see, HttpContext.Current.Request.RawUrl
will not match any of your actions. Try writing:
Html.ActionLink(page.ToString(),
"TheNameOfTheActionMethod",
new { page = page });
Url.Action("ActionMethodName", new { page = i });
Chris,
By reading your "clarifications" and comments above I think you just overcomplicate things around.
Most clear thing that is recognizable between the lines is that you want somewhat reusable widget showing you the paginable list of customers.
So why not just simply:
- Create
CustomerController
, and anIndex()
as an action to show list of customers - Map routes
"customers"
and"customers/page{page}"
toCustomerController.Index()
- Use
Html.RenderAction("Index", "Customer", new {page = page})
wherever you need the list of customers to be rendered.
Following this way all the heavy lifting (including URL resolution) will be made for you by infrastructure and you'll get your "reusable widget" or "control".
As for mentioned HttpContext.Current.Request.RawUrl
stuff - you have to decide: you either want to use ASP.NET routing or build all the routes by yourself. Because these techniques are somewhat excluding each other.
P.S. Anyway, more details on what exactly you want to achieve will help us to help you.
I think you have your routing set up improperly -- probably a parameter in the wrong place in one of your routes. The reason I say this is because it appears that the ToString method of the UrlParameter class itself is being called, resulting in the name of the class being output.
Paging Route (from comments) -- this assumes that the "list" action is "index" and you don't want the index action to appear in the route. Note that it inherits the controller from the current request and, since the action is by convention, you don't need to supply it. Completely untested.
routes.MapRoute(
"Pager", // Route name
"{controller}/page{page}", // URL with parameters
new
{
controller = "Home",
action = "Index",
page = UrlParameter.Optional
} // Parameter defaults
);
Used as:
<%= Html.RouteLink( page, "Pager", new { page = page }, null ) %>
精彩评论