i have 2 link on page PictureList
<%= Html.ActionLink("pic", "PictureList", "Admin")%>
and
<%= Html.ActionLink("pic", "PictureList", "Admin", new { id = item.MenuId })%>
after click on second link, first link catch id parameter. how can it be solved?
I think, your link's redirecting to same action. So you may want to assign the value of id
<%= Html.ActionLink("pic", "PictureList", "Admin", new { id = 0 })%>
Related
I am trying to present a form to my user where they can enter a start postcode, hit a button and end up on a page offering driving directions to a known postcode.
The route I want to match is defined as:
routes.MapRoute("Directions", "Directions/{Name}/{StartPostCode}-to-{DestinationPostCode}", new { controller = "Directions", action = "Index" });
I am using the following code to present the form:
#using (#Html.BeginForm("Index", "Directions", new { DestinationPostCode = Model.postcode, Name = Model.nameURLized }, FormMethod.Get))
{
<input type="text" name="StartPostCode" id="StartPostCode" class="inputGreyed" value="Enter Postcode" onclick="ToggleDefaultText(this)"/>
<input type="submit" value="Get Directions" />
}
The problem is that I end up at /Directions/Index?StartPostCode=abc123. It is missing the destination postcode and the name key value pairs. This of course means I end up with my request being processed by the wrong route. I have proved this using Phil Haacks route debugger.
I have tested going directly to /Directions/TheName/ABC123-to-DT83PX which works as expected. In fact, I tried this using the following code to build a link:
#Html.ActionLink("Directions Generated", "Index", "Directions", new { StartPostCode = "ABC123", DestinationPostCode = Model.postcode, Name = Model.nameURLized }, new { #class = "button", #title = "More details on " + Model.name })
Any help will be much appreciated.
Please do the following:
a) add the default route:
routes.MapRoute(null, "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }
b) Pass the values for DestinationPostCode and ListingName in hidden input fields.
c) Use:
#using (#Html.BeginForm("Index", "Directions")) { your code }
How can I force URL first page without page number?
Here is the exact code I use.
routes.MapRoute("MyPictureQuotes",
"picture-quotes/{PictureQuotesPage}",
new { controller = "Quote", action = "PictureQuotes", PictureQuotesPage = UrlParameter.Optional }
);
<%= Html.RouteLink("Picture Quotes", "MyPictureQuotes", null, new { title = "Picture Quotes", PictureQuotesPage = string.Empty })%>
It returns
"/picture-quotes/5" instead of
"/picture-quotes" from the page
"http://localhost:2489/picture-quotes/5"
It seems the routing value page 5 is carried over.
Does anyone have a solution for this?
You are not using the correct overload of Html.RouteLink. Use the following:
<%= Html.RouteLink("Picture Quotes", "MyPictureQuotes",
new { PictureQuotesPage = string.Empty },
new { title = "Picture Quotes" })%>
My original question was a bit confusing, so let me edit this post to be more clear:
How can I generate an ActionLink that not only uses the Routing engine, but also takes you to an html element with particular ID on a page? I want to generate something like this:
<a href="/ControllerName/ActionName/#section2>Link</a>
or sometimes the same but with an action parameter of 15 for example:
<a href="/ControllerName/ActionName/15#section2>Link</a>
Where the View served by the actionName has an element with the id of "section2":
<div id="section1">
<h1>Section 1</h1>
...
</div>
<div id="section2">
<h1>Section 2</h1>
...
</div>
I believe that I could write a Html helper method that uses Url.RouteUrl() and then append the "#sectionId" but is this the easiest/best way to do this? Should I create a routing rule for this? The default route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Experiment", action = "Index", id = "" }
);
allows me to manually add "#section2" to the end of the url in my browser and it takes me to the section2 div. It seems like rather than adding or changing a route I simply need to append "#section2" to the ActionLink url.
You would want to change/add a route to be:
routes.MapRoute("MyRoute", "{controller}/{action}#{id}", /* some default here */);
Then the MVC Helper would be:
<%= Html.ActionLink("Link Text", "Index", new { id = "sectionId" }) %>
For the following route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{section}",
new { controller = "Home", action = "Index", id = "", section = "" }
);
you would do:
<%= Html.ActionLink("Link Text", "Index", new { section = "#sectionId" }) %>
I think the OP wants the ID to be the anchor ref at the end of the URL, as it appears to be a list of items and he wants the browser to scroll down to the item. (I'm just guesing here because of the "#" stipulation. Small edit to Mark's code:
routes.MapRoute(
"Default",
"{controller}/{action}/#{sectionId}",
new { controller = "Home", action = "Index", sectionId = Model.SectionId } );
I have the following route definition in a MapRoute Table:
routes.MapRoute(
"ViewDocument",
"browse/document/{document_id}/{document_title}",
new { controller = "Document", action = "ViewDocument"}
);
I have to create links of documents on document index view (document object have "id" and "title" property)
What should be my approach to generating the link in ASP.NET MVC?
Is there anything I am doing wrong with the route definition?
In your routes:
routes.MapRoute(
"ViewDocument",
"browse/document/{document_id}/{document_title}",
new { controller = "Document", action = "Title", document_id = "", document_title = ""}
);
In your View:
<%= Url.RouteUrl("ViewDocument", new { document_id = ... , document_title = ... }) %>
(renders plain url)
or
<%= Html.RouteLink("ViewDocument", new { document_id = ... , document_title = ... }) %>
(renders <a></a> element with href attribure filled with the url)
Won't you be able to find the proper Document simply based off its ID?
Won't the Title be redundant?
You can generate links to documents for the route given with the following:
<%= Html.ActionLink("Doc Link", "Title", "Document", new { document_id="id", document_title="title" }, null) %>
A couple of things to be aware of:
Your custom route must be added before the Default route.
You have to include the route values as shown above in order to have them specified in the link.
I am trying to implementation the same pagination that is used in the NerdDinner ASP.NET. I am receiving the following error in my view, whenever the pagination starts to kick in.
"A route named 'Index' could not be
found in the route collection."
The error is happening on Line 64.
Line 62: <% if (this.Model.HasNextPage)
Line 63: { %>
Line 64: <%= this.Html.RouteLink("Next Page >>>", "Index", new { page = (this.Model.PageIndex + 1) })%>
Line 65: <% } %>
Line 66: </div>
My controller code is:
[Authorize]
public ActionResult Index(int? page)
{
const int pageSize = 25;
var topics = this.TopicRepository.FindAllTopics();
var paginatedTopics = new PaginatedList<Topic>(topics, page ?? 0, pageSize);
return this.View(paginatedTopics);
}
My view code is ...
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CreativeLogic.Sauron.WebMvc.Helpers.PaginatedList<CreativeLogic.Sauron.WebMvc.Models.Topic>>" %>
<!-- Code to display the list here -->
<div class="pagination">
<% if (this.Model.HasPreviousPage)
{ %>
<%= this.Html.RouteLink("<<< Previous Page",
"Index", new { page = (this.Model.PageIndex - 1) }) %>
<% } %>
<% if (this.Model.HasNextPage)
{ %>
<%= this.Html.RouteLink("Next Page >>>",
"Index", new { page = (this.Model.PageIndex + 1) })%>
<% } %>
</div>
This is my first attempt at doing the pagination in ASP.NET MVC ... if there is a better way, please let me know, otherwise, where am I going wrong here?
Thanks much!
You should not use RouteLink (which takes a route name) but instead use an ActionLink which takes an action name like Index.
Well the RouteLink Extension Method looks for a defined route with the name of "Index" in the Global.asax, and by default there is just 1 route defined in the Global the "Default", it looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Maybe as HakonB said you must use the ActionLink Extension Method or define a route in the Global asax for the pagination.