master page link generation wrong - s#arp architecture - asp.net-mvc

I am using s#arp architecture 2.0 with asp.net mvc 3.0. The razor code in a master page looks like this:
#Html.ActionLink("Logout", "LogOff", "Users", new { style = "color:Blue;" })
For some reason the app does not produce the correct link anymore (to the action logoff of controller users) but rather points to the current controller for the action logoff. I have not changed anything. where do I have to dig to overcome this please?
The generated link looks like this:
CurrentControllerName/LogOff?Length=5

You are calling a wrong overload of the ActionLink helper. Here's what you do:
#Html.ActionLink(
"Logout", // linkText
"LogOff", // actionName
"Users", // routeValues
new { style = "color:Blue;" } // htmlAttributes
)
It's pretty obvious why this doesn't produce the correct url. You are passing "Users" which is a string value at the place where the helper expects routeValues which must represent an anonymous object.
The correct overload is:
#Html.ActionLink(
"Logout", // linkText
"LogOff", // actionName
"Users", // controllerName
null, // routeValues
new { style = "color:Blue;" } // htmlAttributes
)

Related

asp.net mvc optional params in razor actionLink

I have 2 links for language switch
<a class="dropdown-item"
href="#Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "en" }, null)"
style="color:#333;">English</a>
<a class="dropdown-item"
href="#Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "ar" }, null)"
style="color:#333;">Arabic</a>
it works fine there is only controller and action in url
but when there is optional param like id for detail and edit action than it do not work as expected.
I think I have to change null (this last param) with something but I am new and googled a lot but not getting anything worthy, Please help me.
It would be better if the solution work for n number of optional params instead of only one Id, but for now that will also be acceptable.
it would be better if the solution work for n number of optional params instead of only one Id
for getting all passed querystring parameter on MVC controller side better to use
Request.QueryString
Request.QueryString is NameValueCollection and you get value passed in querystring parameter in your actionlink.
i have tried that looks like below
<a class="dropdown-item" href="#Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "ar",id="100",studentid = 1,studentName = "abc" }, null)" style="color:#333;">Arabic</a>
and your mvc Controller look like below
public ActionResult About(int id)
{
var querystring = Request.QueryString;
// in querystring you get all value like below screenshot
var studentName = querystring["studentName"]; // access parameter like
ViewBag.Message = "Your application description page.";
return View();
}
you can get all parameter that you passed in your controller.

Wrong parameter in Query ActionLink MVC [duplicate]

I'm VERY confused as to why this code
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
results in this link:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
The hidefocus part is what I was aiming to achieve, but where does the ?Length=4 come from?
The Length=4 is coming from an attempt to serialize a string object. Your code is running this ActionLink method:
public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
This takes a string object "Home" for routeValues, which the MVC plumbing searches for public properties turning them into route values. In the case of a string object, the only public property is Length, and since there will be no routes defined with a Length parameter it appends the property name and value as a query string parameter. You'll probably find if you run this from a page not on HomeController it will throw an error about a missing About action method. Try using the following:
Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })
The way I solved this is was adding a null to the fourth parameter before the anonymous declaration (new {}) so that it uses the following method overload: (linkText, actionName, controllerName, routeValues, htmlAttributes):
Html.ActionLink("About", "About", "Home", null, new { hidefocus = "hidefocus" })
You forgot to add the HTMLAttributes parm.
This will work without any changes:
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" },null)
The parameters to ActionLink are not correct, it's attempting to use the "Home" value as a route value, instead of the anonymous type.
I believe you just need to add new { } or null as the last parameter.
EDIT: Just re-read the post and realized you'll likely want to specify null as the second last parameter, not the last.
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" }, new { })
This will take the overload:
string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes
Just remove "Home" (name of the controller) so that the code would be:
Html.ActionLink("About", "About", new { hidefocus = "hidefocus" })
Kindly use right overloaded method with five (5) parameters. Example:
#using (#Ajax.BeginForm("Register", "Account", null,
new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "OnSuccess",
OnFailure = "OnFailure",
OnBegin = "OnBegin",
OnComplete = "OnComplete"
}, new { #class = "form-login" }))
This worked fine
#Html.ActionLink("Informationen", "About", "Home", new { area = "" }, new { #class = "nav-link" })
added new { area = "" }.
As Jonathon Watney pointed out in a comment, this also goes for
Html.BeginForm()
methods. In my case, I was in a Create.cshtml targeting the post request of the corresponding controller + Create action and had
using (Html.BeginForm("Create")) {
#Html.AntiForgeryToken()
...
}
which was adding the querystring "?Length=6" to the form action when rendered. Hinted by roryf's approved answer and realizing the string length of "Create" is 6, I finally solved this by removing the explicit action specification:
using (Html.BeginForm()) {
#Html.AntiForgeryToken()
...
}
With attribute names:
#Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)
Perhaps others had the same issue and need to supply a class value via HTMLAttributes parm.
Here's my solution:
#Html.ActionLink("About", "About", new { controller = "Home", area = "" }, new { hidefocus = "hidefocus", #class = "nav-item nav-link" })
Search for an answer to my question landed me here, basically it's the selection of correct overload of #Html.ActionLink
which matters.
I was selecting an overload which didn't exist, (without the last null), and MVC had no such overload, resulting in a false URL something like the OP mentioned.
A personal note: you can use anonymous types doesn't mean you can use any of the overloads- which do not exist? - make certain: it has to be defined!
- Came here in times of MVC 5.2

Error on Html.ActionLink

I have an action link that when a user clicks on it, it redirects to an mvc view, the actionlink is below,
<%=Html.ActionLink("Select", "Review?usrItId=" + drResponse["ItineraryId"].ToString() + "&Type=" + drResponse["FareType"].ToString(), "", new { #class = "fCheck" })%>
but when the user clicks on it i get the below error,
system.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (?)
the HTML is presented like this:
<a class="fCheck" href="/controller/Review%3fusrItId%3dsi1000%26Type%3dNoFrills?Length=0">Select</a>
thanks in advance for the help. I am using MVC 3, .NET 3.5
The query parameters (userItId and Type here) need to be specified in a different way. It is what the routeValues argument of ActionLink is for:
<%=Html.ActionLink("Select", "Review", new { usrItId = drResponse["ItineraryId"].ToString(), Type = drResponse["FareType"].ToString() }, new { #class = "fCheck" })%>
Try to change your action link to this:
#Html.ActionLink("Select", "Review",
new { usrItId = drResponse["ItineraryId"].ToString(), Type = drResponse["FareType"].ToString()},
new {#class = "fCheck"})

MVC3 Weird behavior of ActionLink with HTML 5 Data- attributes

When I use this helper method to create a link, the data attribute shows up correctly in HTML code:
#Html.ActionLink("Test", "Index", null, new { data_something = "123" })
The HTML is correct:
<a data-something="123" href="/">Test</a>
When I use the following overload of the ActionLink method (I use the T4MVC script, http://mvccontrib.codeplex.com/wikipage?title=T4MVC), the data attribute contains an underscore instead of a dash:
#Html.ActionLink("Test", MVC.Home.Index(), new { data_something = "123" })
The HTML is incorrect:
<a data_something="123" href="/">Test</a>
Is this a know bug or a feature? I searched the bugtracker (http://aspnet.codeplex.com/workitem/list/basic) but was not able to find a corresponding issue.
The following overload is working again, but I don't like to create Dictonaries all the time:
#Html.ActionLink("Test", MVC.Home.Index(), new Dictionary<string, object> {
{ "data-something", "123" }
})
for data attribute use #data_something="123" like
#Html.ActionLink("Test link",
MVC.Home.Index(),
new {controller="Home"}},new {#data_something="123"})
the above code should output
Test Link>

URL parameters not appearing in ASP.MVC when using variant of Html.BeginForm

I've got a view that defines a form as
<% using (Html.BeginForm( "Update", "CcisCase", FormMethod.Post, new { id = "ccisEditForm" } ))
with a submit button:
In the RegisterRoutes method (in the HttpApplication-derived class in global.asax.cs), I've got:
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
routes.MapRoute(
"CcisCase",
"CcisCase/{action}/{cmDatabaseId}/{caseId}",
new { Controller = "CcisCase", Action = "CcisCaseEdit", caseId = "" } );
The url generated by MVC ends with "/Update" but there are no parameters. What am I doing wrong?
Thanks,
Bob
What parameters are you expecting to see? A post does not append parameters to the querystring, a FormMethod.Get would. And, that overload with the id is the collection of HTML attributes to render for the tag (which I'm assuming you knew, but just in case).
HTH.
Your route contains a parameter {caseId} but your BeginForm only defines an id value.
new {id = "cssEditForm"}
You need something like this to include the caseId value
using (Html.BeginForm( "Update", "CcisCase", FormMethod.Post, new { caseId = 1, id = "ccisEditForm" }
If your action isn't using the id="ccisEditForm" value then you can remove that for less code clutter.
I figured out what my problem was. I had to pass the existing route data as follows:
using (Html.BeginForm( "Update", "CcisCase", ViewContext.RouteData.Values, FormMethod.Post, new Dictionary<string, object> { { "id", "ccisEditForm" } } ))

Resources