Hyperlink to call action method - asp.net-mvc-3

I'm trying to figure out a trivial creation of a hyperlink (either through using an Html.ActionLink or plain hyperlink markup) to call a certain controller action and pass in a userId
In have a controller with an Index action. I have a hyperlink in another unrelated view where I need to all that controller's index action and pass in a userId which is param in the action method.
Update:
Html.ActionLink("Test Something", "Index", "Tests", new { #userId = #Model.User.UserId }, null)
that is an example of what I tried. Problem is, it's not showing Tests/ in the url when I moue over the url during runtime

You could use the ActionLink helper from any view anywhere in the application:
#Html.ActionLink(
"call index", // linkText
"index", // action
"home", // controller
new { userid = "123" }, // routeValue
null // htmlAttributes
)
Assuming default routes this should generate:
call index

Related

Make link Ajax.ActionLink in MVC 4

Beeing new to Ajax i need help with how to add the querystring and invoke the Index action without a postback. Now the link looks like this:
#sectionGroup.Term, #sectionGroup.Count
My guess is using Ajax.ActionLink but how do i create the querystring?
Everything that you add to the routeValues and doesn't match a route segment will be added to the querystring.
Using your example, it would look something like this with Ajax.ActionLink:
#Ajax.ActionLink(
sectionGroup.Term + ", " + sectionGroup.Count,
"Index",
new { section = sectionGroup.Term },
new AjaxOptions { UpdateTargetId = "id-of-container" }
)
In your Index action you need to return a partial view when it is requested with ajax. This will remove the layout (master page) from the response.
if (Request.IsAjaxRequest())
return PartialView(model);
return View(model);
Remember to add a reference to the jquery.unobtrusive-ajax.js file.

In MVC3, How can I pass multiple values to a querystring parameter by ActionLink?

In my MVC3 project I have a controller "Test" with this method for Index action
Function Index(fields() As String) As ViewResult
...
Then on the View I have a a multi-select drop down list
#Html.ListBox("fields", New MultiSelectList(my_list, "Value", "Text"))
and everything works fine, however when I try to create an Actionlink passing multiple values of parameter "fields" in the querystring like this
#Html.ActionLink("TestLink", "Index", New With {
.fields = "value1&fields=value2"})
I get the following broken link in the HTML source with the querystring encoded
TestLink
How can I pass multiple values to a querystring parameter by ActionLink?
If you want to do this dynamically by fetching the values from the listbox simply put the listbox inside a form whose action is pointing the controller action you want to invoke and then use a submit button. HTML will then do the job for you.
If you want to generate an ActionLink you could use the following:
#Html.ActionLink(
"TestLink",
"Index",
New RouteValueDictionary() From {
{ "fields[0]", "value1" },
{ "fields[1]", "value2" }
}
)

Html.ActionLink equivalent of a specific URL format

I want to use Html.ActionLink to return an anchor tag with href like /Product/22/Product-Name, where Product is my controller name and 22 is my id and Product-Name is my title.
In my global.asax, I have set the following route :
routes.MapRoute(
"ProductRoute", // Route name
"Product/{id}/{title}", // URL with parameters
new { controller = "Product", action = "Index", id = UrlParameter.Optional });
I tried using following code
#Html.ActionLink(model.Name, null, "Product",
new { id = model.Id, title = model.Name.Replace(" ","-") }, null)
but it yields Product/Browse/22?title=Product-Name. This code is in Browse View returned by Browse action of Catalog controller and that's why it picks up the Browse in between.
Strangely, the same code works fine in the Index View returned by Index action of the Home Controller. What's the reason for this difference? . Am I missing something or this is a bug in Asp.net MVC3.
What Should I do to achieve my stated URL format. For the meantime, I am using this code
#product.Name
make sure you have defined the custom route before the default route and try
#Html.ActionLink(model.Name,
"Index",
new {controller="Product", id = model.Id, title = model.Name.Replace(" ","-") }, null)
the first parameter is the DisplayName, second the ActionResult's name and then the RouteValues

ASP.NET MVC 3 – How can I route directly to an action

I have a controller called HomeController with the actions Index(), MethodOne() and MethodTwo(). Each of these just return View() for now.
When I create an action link with the code:
#Html.ActionLink("Home", "Index", "Home")
I get the hyperlink:
Home
This is good however using the same ActionLink extention method to route too MethodOne I get the hyperlink:
MethodOne</li>
What do I need to configure so my site will accept the hyperlink “/MethodOne”, this currently returns a 404.
I have a suspicion this may be related to routing. My routing table is currently the default:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
You need a route like:
routes.MapRoute(
"MethodOne", //Your route name.
"methodone", //What your browser/user will see at the URL bar: localhost.com/methodone
new { controller = "Home", action = "MethodOne" } //What MVC3 will route to.
);
That will map and catch the request to the Home/MethodOne action method.

MVC Html.ActionLink not passing querystring properly

This seems like it should be pretty straight forward, but I'm apparently confused.
I have a List view that displays a paged list. At the bottom I have a set of actionlinks:
<%= Html.ActionLink("First Page", "List", new { page = 1} ) %>
<%= Html.ActionLink("Prev Page", "List", new { page = Model.PageNumber - 1 }) %>
<%= Html.ActionLink("Next Page", "List", new { page = Model.PageNumber + 1 }) %>
<%= Html.ActionLink("Last Page", "List", new { page = Model.LastPage } )%>
I'm using the basic default routes setup, except with "List" substituted for "Index":
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
The problem is that the ActionLink helpers are generating links of the form:
http://localhost:2083/Retrofit?page=2
rather than
http://localhost:2083/Retrofit/?page=2
(with the trailing slash after the controller name & before the query string). When the first URL is routed, it completely loses the query string - if I look at Request.QueryString by the time it gets to the controller, it's null. If I enter the second URL (with the trailing slash), it comes in properly (i.e., QueryString of "page=2").
So how can I either get the ActionLink helper to generate the right URL, or get the Routing to properly parse what ActionLink is generating?
Thanks.
You need to choose one of two actions:
Modify your route to take a page parameter instead of the default id, and modify the signature of your action method accordingly.
Modify the calls to Html.AcitonLink() so they name the parameter id - that is, change new { page = ... } to new { id = ... }.
This is an old question but for others I thought I would add the answer you are looking for is add both to the routeValues parameter like
Html.ActionLink("Last Page", "List", new { id = Model.ID, page = Model.LastPage } )

Resources