Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ajax.ActionLink?

First of all, where is the documentation for Ajax.* methods in asp.net mvc?

Can Ajax.ActionLink be used to call an action, get a partial view, open a modal window and put the content in it?

like image 679
Shawn Mclean Avatar asked Apr 07 '11 19:04

Shawn Mclean


People also ask

How does HTML ActionLink work?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

How do I post on ActionLink?

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 submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.

How do I pass an object in ActionLink?

If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).


1 Answers

Sure, a very similar question was asked before. Set the controller for ajax requests:

public ActionResult Show() {     if (Request.IsAjaxRequest())      {         return PartialView("Your_partial_view", new Model());     }     else      {         return View();     } } 

Set the action link as wanted:

@Ajax.ActionLink("Show",                   "Show",                   null,                   new AjaxOptions { HttpMethod = "GET",                   InsertionMode = InsertionMode.Replace,                   UpdateTargetId = "dialog_window_id",                   OnComplete = "your_js_function();" }) 

Note that I'm using Razor view engine, and that your AjaxOptions may vary depending on what you want. Finally display it on a modal window. The jQuery UI dialog is suggested.

like image 86
Felix Martinez Avatar answered Sep 19 '22 14:09

Felix Martinez