I have an MVC 3 view with the following code:-
#using (Html.BeginForm(MVC.Order.SearchResults(), FormMethod.Get))
{
#Html.AntiForgeryToken()
#Html.Button("btnSearch", "Search", HtmlButtonType.Submit, null, new { #class = "button primary icon search", alt = "Search the orders (up to 50 characters)" }
}
When I post the form I see the __RequestVerificationToken= and the contents of the verifcation token within the querystring.
Any ideas why this may be the case and how to sort it?
Anti forgery token work only with POST requests. If you want to use them you need to change the verb used of the form to POST instead of GET:
#using (Html.BeginForm(MVC.Order.SearchResults(), FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.Button("btnSearch", "Search", HtmlButtonType.Submit, null, new { #class = "button primary icon search", alt = "Search the orders (up to 50 characters)" }
}
There is a workaround how you can pass antiforegy value through GET method or even in headers. More details here.
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" })%>
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 })%>
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 } );
Currently when I want to set html attributes like maxlength and autocomplete, I have to use the following syntax:
<%= Html.TextBox("username", ViewData["username"], new { maxlength = 20, autocomplete = "off" }) %>
Is there any way to do this without having to explicitly set the ViewData["username"] portion? In other words, I want to rely on the helper method's automatic loading routine rather than having to explicitly tell it which field to load up from the ViewData.
Just pass "null" as second parameter:
<%= Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) %>
yes but you have to use ViewData.Model instead of ViewData.Item()
the code in your controller should look like this (sry 4 VB.NET code)
Function Index()
ViewData("Title") = "Home Page"
ViewData("Message") = "Welcome to ASP.NET MVC!"
Dim user As New User
Return View(user)
End Function
now you can do this in the view
<%=Html.TextBox("username", Nothing, New With {.maxlength = 30})%>
note that the user object has a public property username
hth
I used construction as below:
<%= Html.TextBox("username", "", new { #maxlength = "20", #autocomplete = "off" }) %>
For Setting max length of TextBox you can pass "" or null for Second Parameter and set html attributes(maxlength) as third parameter
<%=Html.TextBox("username", "", new { #maxlength = 10 }) %>