This article explains how to use the partial view and AjaxBegin Form to load data without a page reference. I have explained how to insert and list code. See the following procedure.

Step 1

First create the database and create a table depending on you needs.

Step 2

Create an Entity Data Model from the database. This is explained in detail in my previous blog.

Step 3

First you create a controller with the name “InformationController” in the controller folder.

Step 4

In the controller you need to create an Action method. This is the get method.

  1. public ActionResult AddInformation()
  2. {
  3. ViewBag.CountryList = _datacontax.Countries.ToList().Select(s => new SelectListItem { Text=s.CountryName,Value=s.CountryName});
  4. ViewBag.StateList = _datacontax.States.ToList().Select(s => new SelectListItem {Text=s.StateName,Value=s.StateName });
  5. ViewBag.CityList = _datacontax.Cities.ToList().Select(s => new SelectListItem {Text=s.CityName,Value=s.CityName});
  6. return View();
  7. }

Step 5

Create an "AddInformation.chtml" view. Right-click on the Action method and Add View.

add view

Step 6

Now write the code in the View as in the following. You need to provide the namespace as in the following.

  1. @model imaguploaddemo.Models.demoregistartion
  2. @{
  3. ViewBag.Title = "AddInformation";
  4. Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6. @using (Ajax.BeginForm("AddInformation", "Information", new AjaxOptions { UpdateTargetId = "InformationAjaxList", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnSuccess = "ClearAll();" }, new { @class = "form-horizontal" }))
  7. {
  8. @Html.AntiForgeryToken()
  9. <h4>Create a new account.</h4>
  10. <hr />
  11. @Html.ValidationSummary()
  12. <div class="form-group">
  13. @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
  14. <div class="col-md-10">
  15. @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
  16. @Html.ValidationMessageFor(model => model.FirstName, "", new { @style = "color:red;" })
  17. </div>
  18. </div>
  19. <div class="form-group">
  20. @Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" })
  21. <div class="col-md-10">
  22. @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
  23. </div>
  24. </div>
  25. <div class="form-group">
  26. @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
  27. <div class="col-md-10">
  28. @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
  29. @Html.ValidationMessageFor(model => model.FirstName, "", new { @style = "color:red;" })
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
  34. <div class="col-md-10">
  35. @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
  36. @Html.ValidationMessageFor(model => model.FirstName, "", new { @style = "color:red;" })
  37. </div>
  38. </div>
  39. <div class="form-group">
  40. @Html.LabelFor(m => m.EmailID, new { @class = "col-md-2 control-label" })
  41. <div class="col-md-10">
  42. @Html.TextBoxFor(m => m.EmailID, new { @class = "form-control" })
  43. @Html.ValidationMessageFor(model => model.FirstName, "", new { @style = "color:red;" })
  44. </div>
  45. </div>
  46. <div class="form-group">
  47. @Html.LabelFor(m => m.Gender, new { @class = "col-md-2 control-label" })
  48. <div class="col-md-10">
  49. @Html.RadioButtonFor(model => model.Gender, "Male") Male
  50. @Html.RadioButtonFor(model => model.Gender, "Female") Female
  51. @Html.ValidationMessageFor(model => model.Gender, "", new { @style = "color:red;" })
  52. </div>
  53. </div>
  54. <div class="form-group">
  55. @Html.LabelFor(m => m.Country, new { @class = "col-md-2 control-label" })
  56. <div class="col-md-10">
  57. @Html.DropDownListFor(m => m.Country, (IEnumerable<SelectListItem>)ViewBag.CountryList, "Select", new { @id = "ddlCountry" })
  58. @Html.ValidationMessageFor(model => model.Country, "", new { @style = "color:red;" })
  59. </div>
  60. </div>
  61. <div class="form-group">
  62. @Html.LabelFor(m => m.State, new { @class = "col-md-2 control-label" })
  63. <div class="col-md-10">
  64. @Html.DropDownListFor(m => m.State, (IEnumerable<SelectListItem>)ViewBag.StateList, "Select", new { @id = "ddlState" })
  65. @Html.ValidationMessageFor(model => model.State, "", new { @style = "color:red;" })
  66. </div>
  67. </div>
  68. <div class="form-group">
  69. @Html.LabelFor(m => m.City, new { @class = "col-md-2 control-label" })
  70. <div class="col-md-10">
  71. @Html.DropDownListFor(m => m.City, (IEnumerable<SelectListItem>)ViewBag.CityList, "Select", new { @id = "ddlCity" })
  72. @Html.ValidationMessageFor(model => model.City, "", new { @style = "color:red;" })
  73. </div>
  74. </div>
  75. <div class="form-group">
  76. @Html.LabelFor(m => m.Hobby, new { @class = "col-md-2 control-label" })
  77. <div class="col-md-10">
  78. @Html.TextBoxFor(m => m.Hobby, new { @class = "form-control" })
  79. @Html.ValidationMessageFor(model => model.Hobby, "", new { @style = "color:red;" })
  80. </div>
  81. </div>
  82. <div class="form-group">
  83. <div class="col-md-offset-2 col-md-10">
  84. <input type="submit" class="btn btn-default" value="Register" />
  85. </div>
  86. </div>
  87. }
  88. <div id="InformationAjaxList">
  89. @Html.Action("_InformationList")
  90. </div>
  91. @section Scripts {
  92. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
  93. @Scripts.Render("~/bundles/jqueryval")
  94. <script>
  95. function ClearAll()
  96. {
  97. $("#FirstName").val('')
  98. $("#LastName").val('')
  99. $("#UserName").val('')
  100. $("#Password").val('')
  101. $("#EmailID").val('')
  102. $("#Gender").val('')
  103. $("#Country").val('')
  104. $("#State").val('')
  105. $("#City").val('')
  106. $("#Hobby").val('')
  107. }
  108. </script>
  109. }
Step 7

The following code is for the partial view. You need to create a partial view. Before creating the partial view you need to write an action method as in the following:
  1. public ActionResult _InformationList()
  2. {
  3. var model = _datacontax.demoregistartions.ToList();
  4. return PartialView(model);
  5. }
Step 8

I have the following code in "AddInformation.cshtml".
  1. public ActionResult _InformationList()
  2. {
  3. var model = _datacontax.demoregistartions.ToList();
  4. return PartialView(model);
  5. }
Step 9

Write the following code in the Partial view. This is for the listing information.
  1. @model IEnumerable<imaguploaddemo.Models.demoregistartion>
  2. <div class="panel-body">
  3. <div>
  4. <a href="/FormInformation/FormInformation">Add Form Detail</a>
  5. </div>
  6. <table class=" table-bordered table-responsive">
  7. <tr>
  8. <th>First Name</th>
  9. <th>Last Name</th>
  10. <th>Email ID</th>
  11. </tr>
  12. @foreach (var v in Model)
  13. {
  14. <tr>
  15. <td>@v.FirstName</td>
  16. <td>@v.LastName</td>
  17. <td>@v.EmailID</td>
  18. <td>
  19. <a href="/Registration/[email protected]">Edit</a>
  20. <a href="/Registration/[email protected]">Delete</a>
  21. </td>
  22. </tr>
  23. }
  24. </table>
  25. </div>
Step 10

Write the Action method for the post.
  1. [AcceptVerbs(HttpVerbs.Post)]
  2. public ActionResult AddInformation(demoregistartion objdemoreg)
  3. {
  4. objdemoreg.IsActive = true;
  5. objdemoreg.UserID = Guid.NewGuid();
  6. _datacontax.demoregistartions.Add(objdemoreg);
  7. _datacontax.SaveChanges();
  8. return RedirectToAction("_InformationList");
  9. }
Step 11

The following is the full code of the controller.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using imaguploaddemo.Models;
  7. namespace imaguploaddemo.Controllers
  8. {
  9. public class InformationController : Controller
  10. {
  11. private DB_9CC4F9_mvcdemoEntities _datacontax;
  12. public InformationController()
  13. {
  14. _datacontax=new DB_9CC4F9_mvcdemoEntities();
  15. }
  16. //
  17. // GET: /Information/
  18. public ActionResult AddInformation()
  19. {
  20. ViewBag.CountryList = _datacontax.Countries.ToList().Select(s => new SelectListItem { Text=s.CountryName,Value=s.CountryName});
  21. ViewBag.StateList = _datacontax.States.ToList().Select(s => new SelectListItem {Text=s.StateName,Value=s.StateName });
  22. ViewBag.CityList = _datacontax.Cities.ToList().Select(s => new SelectListItem {Text=s.CityName,Value=s.CityName});
  23. return View();
  24. }
  25. public ActionResult _InformationList()
  26. {
  27. var model = _datacontax.demoregistartions.ToList();
  28. return PartialView(model);
  29. }
  30. [AcceptVerbs(HttpVerbs.Post)]
  31. public ActionResult AddInformation(demoregistartion objdemoreg)
  32. {
  33. objdemoreg.IsActive = true;
  34. objdemoreg.UserID = Guid.NewGuid();
  35. _datacontax.demoregistartions.Add(objdemoreg);
  36. _datacontax.SaveChanges();
  37. return RedirectToAction("_InformationList");
  38. }
  39. }
  40. }
Step 12

Run the code. The following page displays.

output