21st Chapter URL and Ajax helper methods

Source: Internet
Author: User
Tags jquery library

First, create basic links and URLs

  One of the most basic tasks of a view is to create a link or URL that enables the user to enter other parts of the application with it.

The benefit of the link or URL generated by the helper is that the output comes from the routing configuration when the routing changes are fully automatic reflected in the links and URLs.

HTML helper for rendering URLs

Describe Example Output
URL relative to the application Url.content ("~/content/site.css") /content/site.css
Linking to the specified action controller Html.ActionLink ("My Link", "Index", "Home") <a href= "/" >my Link</a>
Action URL Url.action ("GetPeople", "people") /people/getpeople
URLs that use routing data Url.routeurl (New {controller = "people", action = "GetPeople"}) /people/getpeople
Using links to 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>

Second, MVC's progressive Ajax

  Asynchronous JavaScript and XML: (most importantly) Background request server data without overloading a model of a Web page.

The progressive AJAX features of the MVC framework are based on jquery.

Note: in real-world applications running on a true Internet connection, synchronizing a form may cause users who use the Web application to crash, consuming a large amount of bandwidth and processing power from the server.

    First step: Enable progressive AJAX features in Web. config

<appSettings> <add key= "webpages:enabled" value= "false"/> <add key= "Unobtrusivejavascrip Tenable "value=" true "/> </appSettings>

   Second Step: the header of the template page references the jquery library (min version) and the Jquery.unobtrusive-ajax Library (min version)
      <script src= "~/scripts/jquery-1.7.1.js" ></script>
  <script src= "~/scripts/jquery.unobtrusive-ajax.js" ></script>
Step Three: Modify the initial request method, new action methods (for Ajax) and partial views in the controller
 Public ActionResult GetPeople (string"all")        {            return View ((object) selectedrole);        }
@using helpermethods.models@model string@{viewbag.title = "GetPeople"; ajaxoptions ajaxopts = new Ajaxoptions {Updatetargetid = "tablebody"};}<H2>Get people</H2><Table>    <thead>    <TR>        <th>Name</th>        <th>Role</th>    </TR>    </thead>   <tbody id= "Tablebody" > @Html. Action ("Getpeopledata", New{selectedrole=model}) </tbody></Table>@using (Ajax.beginform ("Getpeopledata", ajaxopts)) {<div> @Html. DropDownList ("Selectedrole", new Select List (new[] {"All"}. Concat (Enum.getnames (typeof (Role)))) <button type= "Submit" > Enquiry </button> </div>}
        PublicPartialviewresult Getpeopledata (stringSelectedrole =" All") {IEnumerable<Person> data =Persondata; if(Selectedrole! =" All") {Role selected= (Role) enum.parse (typeof(Role), selectedrole); Data= Persondata.where (p = = P.role = =selected); }            returnPartialview (data); }
@using Helpermethods.models@model IEnumerable< Person>@foreach (person p in Model) {<TR>        <TD>@p.name</TD>        <TD>@p.role</TD>    </TR>}

Ajaxoptions Property

Property Describe
Confirm Sets the confirmation window information displayed to the user before the AJAX request is formed
HttpMethod Set the HTTP method used to form the request (Get or Post)
Insertionmode Specifies how the content accepted from the server is inserted into the HTML. Enumeration: InsertAfter, InsertBefore, Replace (default)
Loadingelementid Specifies the ID of the HTML element, which is the HTML element to be displayed during the execution of the AJAX request
Loadingelementduration Specifies the duration of the animation for displaying the element specified by the Loadingelementid
Updatetargetid Sets the ID of an HTML element to which the content received from the server is inserted into the element
Url Set the requested server-side URL

Ajaxoptions Callback Property

Property jquery Events Describe
Onbigin Beforesend Call immediately before sending a request
OnComplete Complete Called when the request succeeds
OnFailure Error Called when a request fails
Onsuccess Success Called when the request has completed, regardless of whether the request succeeded or failed

  

Json

Source: Http://yunpan.cn/ccvvsjTacNvWc Access Password 0D10

21st Chapter URL and Ajax helper methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.