Html.ActionLink URL error - asp.net-mvc

return RedirecttoAction("Success")
how to generate redirection?

You are using the wrong overload of Html.ActionLink, you need
<%: Html.ActionLink("linkText", "actionName", "controllerName") %>
Without the third controllerName parameter it will default to the current controller which appears to be in this case your UserController when I expect you want to direct to the AccountController. That is why the ActionLink works in your other view.

Related

Html.BeginForm() with GET method

How can I specify that my form should be using GET method with #Html.BeginForm() ?
#using (Html.BeginForm(method: FormMethod.Get))
Here VS complains that best overload doesn't have a parameter method. Thank you!
There is an overload that allows you to specify the method:
#using (Html.BeginForm("someAction", "someController", FormMethod.Get))
{
...
}
Decorate the controller's action method with [HttpGet]. This is the controller action that this form will submit to.

ViewData as a hyperlink

I an ASP.NET MVC we can pass some data via a ViewData and then show it on a page:
<%: ViewData["Foo"]%>
But how to make a hyperlink out of it?
Something like following:
<%: Html.ActionLink(ViewData["Foo"], "Index", "Home") %>
Cast it to string:
Html.ActionLink((string)ViewData["Foo"], "Index", "Home")
In general, however, try to avoid using ViewData and use a strongly typed ViewModel instead. (Thus, you would have avoided the problem in this question, btw).

How to avoid default URL encoding in ASP.NET MVC Html Helpers like RouteLink

I want my url like this:
"http://domain.com/tag/高兴"
My route mapping:
routes.MapRoute("Tag", "tag/{name}", new { controller = "Tag", action="Index" });
But Html.RouteLink will encode the parameters as default. If I use Html.RouteLink in my View, the generated html is:
高兴
Is there any way to avoid this?
Changed my example.
This works in my case
<%= HttpUtility.UrlDecode(Html.RouteLink("Test", new { id = "高兴" }).ToString())%>
Make sure to change from <%: to <%=

asp.net mvc Html.ActionLink() keeping route value I don't want

I have the following ActionLink in my view
<%= Html.ActionLink("LinkText", "Action", "Controller"); %>
and it creates the following URL http://mywebsite.com/Controller/Action
Say I add an ID at the end like so: http://mywebsite.com/Controller/Action/53 and navigate to the page. On this page I have the markup I specified above. Now when I look at the URL it creates it looks like this:
http://mywebsite.com/Controller/Action/53 (notice the addition of the ID)
But I want it to remove the ID and look like it did originally, like this http://mywebsite.com/Controller/Action (notice no ID here)
Any ideas how I can fix this? I don't want to use hard coded URLs since my controller/actions may change.
The solution is to specify my own route values (the third parameter below)
<%= Html.ActionLink("LinkText", "Action", "Controller",
new { id=string.Empty }, null) %>
It sounds like you need to register a second "Action Only" route and use Html.RouteLink(). First register a route like this in you application start up:
routes.MapRoute("ActionOnly", "{controller}/{action}",
new { controller = "Home", action = "Index" } );
Then instead of ActionLink to create those links use:
Html.RouteLink("About","ActionOnly")
The problem is the built in methods take input from the URL you are currently on as well as what you supply. You could try this:
<%= Html.ActionLink("LinkText", "Action", "Controller", new { id = ""}) %>
That should manually wipe the id parameter.
Don't know why, but it didn't work for me (maybe because of Mvc2 RC). Created urlhelper method =>
public static string
WithoutRouteValues(this UrlHelper helper, ActionResult action,params string[] routeValues)
{
var rv = helper.RequestContext.RouteData.Values;
var ignoredValues = rv.Where(x=>routeValues.Any(z => z == x.Key)).ToList();
foreach (var ignoredValue in ignoredValues)
rv.Remove(ignoredValue.Key);
var res = helper.Action(action);
foreach (var ignoredValue in ignoredValues)
rv.Add(ignoredValue.Key, ignoredValue.Value);
return res;
}
If you either don't know what values need to be explicitly overridden or you just want to avoid the extra list of parameters you can use an extension method like the below.
View
The implementation details are in this blog post
I explicitly set the action name as "Action/". Seems a little like a hack but it's a quick fix.
#Html.ActionLink("Link Name", "Action/", "Controller")
Another way is to use ActionLink(HtmlHelper, String, String, RouteValueDictionary) overload, then there are no need to put null in the last parameter
<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>
The overloads of Html.ActionLink are changed on the later versions of MVC. On MVC 5 and above. This is how to do this:
#Html.ActionLink("LinkText", "Action", "Controller", new { id = "" }, null)
Note I passed "" for id parameter and null for HTMLATTRIBUTES.
I needed my menu links to be dynamic. Rather than implement a lot of extra code and routing for every single page I simple dispensed with the HTML helper.
#item.MenuItemName

ActionUrl in ASP.NET MVC Preview 5

I don't need a Link but rather only the href= part of the ActionLink.
But if I call Html.ActionLink(...) I get a back.
Is there a way to just return the URL of the Action while not getting the ?
MVC also provides a UrlHelper class which can do the same thing:
<%=Url.Action(actionName)%>
<%=Url.Action(actionName, htmlValues)%>
<%=Url.Action(actionName, controllerName, htmlValues)%>
Edit: in response to commment, now including parameters:
<% =Html.BuildUrlFromExpression<YourController>(c => c.YourAction(parameter)) %>

Resources