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?
There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
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.
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.
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With