Chapter 1 URL and Ajax helper methods, Chapter 2 urlajax assistance
1. create basic links and URLs
One of the most basic tasks of a view is to create links or URLs so that users can access other parts of the application.
The advantage of the link or URL generated by the Helper is that the output comes from the route configuration, which is automatically reflected in the link and URL when the route changes.
HTML helper for URL Rendering
Description |
Example |
Output |
Relative to the application URL |
Url. Content ("~ /Content/Site.css ") |
/Content/Site.css |
Link to the specified action Controller |
Html. ActionLink ("My Link", "Index", "Home ") |
<A href = "/"> My Link </a> |
Action URL |
Url. Action ("GetPeople", "People ") |
/People/GetPeople |
Use the URL of the route data |
Url. RouteUrl (new {controller = "People", action = "GetPeople "}) |
/People/GetPeople |
Use the link of Route Data |
Html. RouteLink ("My Link", new {controller = "People", action = "GetPeople "}) |
<A href = "/People/GetPeople"> My Link </a> |
Link to the specified route |
Html. RouteLink ("My Link", "FormRoute", new {controller = "People", action = "GetPeople "}) |
<A href = "/app/forms/People/GetPeople"> My Link </a> |
Ii. Progressive Ajax of MVC
Asynchronous JavaScript and XML:(The Most Important Role) a model that requests server data in the background without reloading the web page.
The progressive Ajax feature of the MVC Framework is based on JQuery.
Note:For actual applications running on real Internet connections, synchronizing forms may cause users using Web applications to crash and occupy a large amount of server bandwidth and processing capabilities.
Step 1: Enable progressive Ajax in web. config
<Deleetask> <add key = "webpages: Enabled" value = "false"/><Add key = "UnobtrusiveJavascriptEnable" value = "true"/></AppSettings>
Step 2: reference the JQuery Library (min version) and jquery. unobtrusive-ajax Library (min version) in the header on the template page)
<Script src = "~ /Scripts/jquery-1.7.1.js "> </script>
<Script src = "~ /Scripts/jquery. unobtrusive-ajax.js "> </script>
Step 3: Modify the initial request method and add action methods (for ajax) and segment views in the controller.
public ActionResult GetPeople(string selectedRole = "All") { return View((object)selectedRole); }
@ Using HelperMethods. Models@ Model string@ {ViewBag. title = "GetPeople"; AjaxOptions ajaxOpts = new AjaxOptions {UpdateTargetId = "tableBody "};}
public PartialViewResult GetPeopleData(string selectedRole = "All") { IEnumerable<Person> data = personData; if (selectedRole != "All") { Role selected = (Role) Enum.Parse(typeof (Role), selectedRole); data = personData.Where(p => p.Role == selected); } return PartialView(data); }
@using HelperMethods.Models@model IEnumerable<Person>@foreach (Person p in Model){ <tr> <td>@p.Name</td> <td>@p.Role</td> </tr>}
AjaxOptions attributes
Attribute |
Description |
Confirm |
Set the confirmation window information displayed to the user before forming an Ajax request |
HttpMethod |
Set the HTTP method (Get or Post) used to form the request) |
InsertionMode |
Specifies how the content accepted by the slave server is inserted into HTML. Enumeration: InsertAfter, InsertBefore, and Replace (default) |
LoadingElementId |
Specifies the ID of the HTML element, which is the HTML element to be displayed during Ajax request execution. |
LoadingElementDuration |
Specifies the animation duration to display the elements specified by LoadingElementId. |
UpdateTargetId |
Sets the ID of the HTML element. The content received from the server is inserted into the element. |
Url |
Set the requested server URL |
AjaxOptions callback Property
Attribute |
JQuery events |
Description |
OnBigin |
BeforeSend |
Call immediately before sending a request |
OnComplete |
Complete |
Called when the request is successful |
OnFailure |
Error |
Call when the request fails |
OnSuccess |
Success |
Called when the request has been completed, regardless of whether the request is successful or failed |
Json
Source code: http://yunpan.cn/ccvvsjTacNvWc access password 0d10