banner



How To Reload And Update Mvc View Automatically When Inputs Change

  • Updated date Dec 25, 2015
  • 483.8k
  • 17

In this article we will acquire how to mail data to controller without folio refresh in ASP.NET MVC using Ajax.BeginForm.

BackgroundPosting information to the server without whole postback or we tin can say without page refresh is very of import to relieve the server resources and make application faster. In ASP.NET MVC there are lot of options to reach this without writing lots of custom code.

Many forum post I read, ane common question was the difference between Html.BeginForm and Ajax.BeginForm when both are doing whole page refresh while posting data to the server and also seen lots of misleading answers, and so by considering above requirement I decided to write this article. So permit us see step by footstep .

Pace 1: Create an MVC application.

  1. "Beginning", and so "All Programs" and select "Microsoft Visual Studio 2015".
  2. "File", then "New" and click "Project" so select "ASP.Cyberspace Spider web Application Template", and so provide the Project a name as you wish and click on OK.
  3. Choose MVC empty application choice and click on OK

Footstep two: Add model course.

Right click on Model folder in the created MVC application and add class named EmpModel and right the following lines of code as.

EmpModel.cs

  1. using  System.ComponentModel.DataAnnotations;
  2. namespace  AjaxBeginFormwithMVC.Models
  3. {
  4. public class  EmpModel
  5.     {
  6.         [Required]
  7. public string  Proper noun { go ; set ; }
  8.         [Required]
  9. public string  Urban center { get ; gear up ; }
  10.         [Required]
  11. public string  Address { get ; set up ; }
  12.     }
  13. }

Step 3: Add Home controller controller.

Right click on Controller folder in the created MVC application and add the controller grade as:

Now after selecting controller template, click on add button then the post-obit window appears,

Specify the controller name and click on add push, At present open up the HomeController.cs file and write the post-obit code into the Home controller course as:


HomeController.cs

  1. using  AjaxBeginFormwithMVC.Models;
  2. using  System.Spider web.Mvc;
  3. namespace  AjaxBeginFormwithMVC.Controllers
  4. {
  5. public class  HomeController : Controller
  6.     {
  7.         [HttpGet]
  8. public  ActionResult EmployeeMaster()
  9.         {
  10. return  View();
  11.         }
  12.         [HttpPost]
  13. public  ActionResult EmployeeMaster(EmpModel obj)
  14.         {
  15.             ViewBag.Records ="Name : "  + obj.Name + " Urban center:  "  + obj.City + " Addreess: "  + obj.Address;
  16. return  PartialView( "EmployeeMaster" );
  17.         }
  18.     }
  19. }

In the above code, we have added EmployeeMaster Action effect method and it returns the partial view named EmployeeMaster with input parameters value.

Step iv:
Add View,


Add strongly typed view named EmployeeMaster from EmpModel class:

After clicking on add push button the view generated .


Step five:
Add Reference of jquery.unobtrusive-ajax.

To work Ajax.BeginForm functionality properly we need to add together the reference of jquery.unobtrusive-ajax library, there are many ways to add the reference of jQuery library into our project. The following are some methods,

  • Using NuGet package manager, you tin install library and reference into the project.

Right click on created MVC project and observe Nuget Bundle manager pick,

Now the post-obit window volition appear and search for the jQuery unobtrusive ajax every bit,

Choose appropriate version and click on Install button. It will install jQuery UI library to your project and library script file get added into the script folder of the created project which you tin add reference into the projection,

  • Use CDN library provided by Microsoft, jQuery, Google or whatever other which requires active internet connectedness.
  • Download library using NuGet and reference into the projection.

The library will be expect like as follows later adding it in script folder.


To work Ajax.BeginForm functionality properly don't forget to add the reference of the following jQuery library as,

  1. <script src= "~/Scripts/jquery-1.x.2.js" ></script>
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js" ></script>

Afterwards adding necessary code, files and logic the EmployeeMaster.cshtml lawmaking will look like the following,

  1. @model AjaxBeginFormwithMVC.Models.EmpModel
  2. @{
  3.     ViewBag.Title ="www.compilemode.com" ;
  4. }
  5. <script src="~/Scripts/jquery-1.ten.two.js" ></script>
  6. <script src="~/Scripts/jquery.unobtrusive-ajax.js" ></script>
  7. <div id="divEmp" >
  8.     @using  (Ajax.BeginForm( "EmployeeMaster" , "Home" , new  AjaxOptions { HttpMethod = "POST" , UpdateTargetId = "divEmp"  }))
  9.     {
  10.         @Html.AntiForgeryToken()
  11.         <divcourse = "class-horizontal" >
  12.             <hr />
  13.             @Html.ValidationSummary(true , "" , new  { @ class  = "text-danger"  })
  14.             <divclass = "form-group" >
  15.                 @Html.LabelFor(model => model.Name, htmlAttributes:new  { @ course  = "control-label col-md-2"  })
  16.                 <divgrade = "col-md-10" >
  17.                     @Html.EditorFor(model => model.Proper noun,new  { htmlAttributes = new  { @ class  = "form-control"  } })
  18.                     @Html.ValidationMessageFor(model => model.Name,"" , new  { @ class  = "text-danger"  })
  19.                 </div>
  20.             </div>
  21.             <divclass = "course-group" >
  22.                 @Html.LabelFor(model => model.City, htmlAttributes:new  { @ class  = "control-characterization col-md-2"  })
  23.                 <divcourse = "col-md-10" >
  24.                     @Html.EditorFor(model => model.City,new  { htmlAttributes = new  { @ course  = "form-control"  } })
  25.                     @Html.ValidationMessageFor(model => model.Urban center,"" , new  { @ class  = "text-danger"  })
  26.                 </div>
  27.             </div>
  28.             <divclass = "form-group" >
  29.                 @Html.LabelFor(model => model.Address, htmlAttributes:new  { @ class  = "control-characterization col-md-2"  })
  30.                 <divclass = "col-doc-10" >
  31.                     @Html.EditorFor(model => model.Accost,new  { htmlAttributes = new  { @ class  = "grade-command"  } })
  32.                     @Html.ValidationMessageFor(model => model.Address,"" , new  { @ course  = "text-danger"  })
  33.                 </div>
  34.             </div>
  35.             <divgrade = "class-group" >
  36.                 <divclass = "col-md-offset-ii col-md-10" >
  37.                     <input type="submit"  value= "Relieve" class = "btn btn-master"  />
  38.                 </div>
  39.             </div>
  40.             <hour />
  41.             <divclass = "form-group" >
  42.                 <divform = "col-md-offset-2 col-md-12 text-success" >
  43.                    @ViewBag.Records
  44.                 </div>
  45.             </div>
  46.         </div>
  47.     }
  48. </div>
  49. <script src="~/Scripts/jquery.validate.min.js" ></script>
  50. <script src="~/Scripts/jquery.validate.unobtrusive.min.js" ></script>

Now run the application and click on save push button without entering text in textbox, it volition burn down the validation as in the following,

From preceding example it is clear that validation also get fired. At present enter the proper details and click on salvage push button, information technology will laissez passer data to the controller asynchronously without whole page refresh as,

From all the in a higher place examples, how to mail information to controller without Folio refresh in ASP.NET MVC using Ajax.BeginForm.


Note:

  • For the complete lawmaking, delight download the sample zip file.
  • You lot demand to use the jQuery library.

Summary

I hope this article is useful for all readers. If you accept a suggestion so please contact me.

Source: https://www.c-sharpcorner.com/UploadFile/0c1bb2/post-data-without-whole-postback/

Posted by: reynoldshomply.blogspot.com

0 Response to "How To Reload And Update Mvc View Automatically When Inputs Change"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel