ASP. NET MVC5 use AjaxHelp and mvc5ajaxhelp

Source: Internet
Author: User

ASP. NET MVC5 use AjaxHelp and mvc5ajaxhelp

By default, Visual Studio 2013 creates an ASP. NET MVC5 project that does not contain jquery. unobtrusive-ajax.js and needs to be manually added.

Click the "Tools" menu in Visual Studio 2013, select "Program Package Manager", and then select "NuGet package for managing solutions". In the displayed dialog box, search for jquery. unobtrusive-ajax.js, and then install it.

See the information. The corresponding version is 3.2.0 and must be supported by jQuery1.8 or later. The default jQuery1.10.2 contained in MVC5 can meet the conditions.

Next, we can use AjaxHelper.

View code:

@ Model MVCLearning. Models. SetPasswordModel @ {ViewBag. Title = "Change Password";} <script type = "text/javascript" src = "@ Url. Content ("~ /Scripts/jquery-1.10.2.js ")"> </script> <script type = "text/javascript" src = "@ Url. Content ("~ /Scripts/jquery. unobtrusive-ajax.js ")"> </script> <script type = "text/javascript"> function Show (msg) {alert (msg) ;}</script> @ using (Ajax. beginForm ("Edit", new AjaxOptions () {OnSuccess = "Show"}) {@ Html. antiForgeryToken () <br/> <div class = "form-horizontal"> @ Html. validationSummary (true) <div class = "form-group"> @ Html. labelFor (model => model. password, new {@ class = "control-label col-md-2"}) <div cl Ass = "col-md-10"> @ Html. passwordFor (model => model. password) @ Html. validationMessageFor (model => model. password) </div> <div class = "form-group"> <div class = "col-md-offset-2 col-md-10"> <input type = "submit" value = "Save" class = "btn-default"/> </div >}@ section Scripts {@ Scripts. render ("~ /Bundles/jqueryval ")}

Don't forget to reference the script in the View:

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script><script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>   


Controller code:

[HttpPost] public ActionResult Edit (SetPasswordModel model) {try {string userName = User. identity. name; var user = db. user. where (x => x. userName = userName ). firstOrDefault (); if (model. password! = User. Password) {ModelState. AddModelError ("", "the original Password is incorrect! "); Return View (model);} else {user. Password = model. NewPassword; // update the Password db. SaveChanges (); return Content (" the Password is successfully modified! ");} // Return RedirectToAction (" Index "," ContactGroup ");} catch {return View (model );}}


You can also directly return JavaScript in the Controller. The Code is as follows:

The View code is modified as follows:

@ Using (Ajax. BeginForm ("Edit", new AjaxOptions ()))

The Controller code is modified as follows:

Return JavaScript ("alert ('password modified successfully! ');");

 


Aspnet mvc 3 Ajax usage and execution Principle

Usage: Use @ Ajax. BeginForm (....)
Execution principle: the form is serialized and Ajax is sent to the server ..

How to Use AJAX in ASPNET ??

You can use ASP. net ajax, which is a set of AJAX implementations integrated by MS in ASP. NET. You can learn the following materials:
Www.asp.net/..t-ajax
Www.asp.net/web-forms/videos/aspnet-ajax
Basic controls such as UpdatePanel are relatively reliable and easy to use. It is no problem to use them in a large amount in projects.
The AJAX Control Toolkit Control is more like learning ASP. net ajax uses sample code, which has many errors and is slow to update. Even if you want to rewrite the source code into what you want, you can have a pot. It is not recommended unless it is available in a simple project.

You can also use other AJAX methods. In my experience, JQuery Ajax runs fast and well in the ASP. NET environment.

Related Article

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.