Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.ActionLink how to add a query string

I need to setup a query string in a View using MVC and Razor.

Here my code

Controller: Home
ActionResult: Daily
QueryString: DateForLookUp 

@Html.ActionLink("Next Day", "Daily", "Home", new { @DateForLookUp = @Model.AddOneDay() })

the result at the moment is

http://mysite.com/Home/Daily?lenght=4

it should be

http://mysite.com/Home/Daily?DateForLookUp=01/01/2014

What I am doing wrong here?

like image 899
GibboK Avatar asked Feb 05 '13 10:02

GibboK


People also ask

What is difference between HTML ActionLink and URL action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is HTML ActionLink ()?

ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, URL fragment, route values, and HTML attributes.

How do I transfer my ActionLink model to my controller?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.


1 Answers

The Html.ActionLink has a lots of overloads.

And you are using the wrong overload which interprets your controller name "Home" as the route values.

One of correct overload/sysntax is:

@Html.ActionLink(
    "Next Day", //linkText
    "Daily", //actionName
    "Home", //controllerName
     new { @DateForLookUp = @Model.AddOneDay() }, //routeValues
     null //htmlAttributes
)
like image 156
nemesv Avatar answered Oct 10 '22 02:10

nemesv