Get Method Call

I have to explain in 3 ways:

  1. Simple Return the Message what you type on Textbox using JsonResult. The .cshtml Page Lokks Like:
    1. <script type="text/javascript">
    2. $(document).ready(function () {
    3. $('#SubmitName').click(function () {
    4. var url = "../Home/InputMessage";
    5. var name = $('#Name').val();
    6. $.get(url, { input: name }, function (data) {
    7. $("#rData").html(data);
    8. });
    9. })
    10. });
    11. </script>
    12. <div>
    13. <p>
    14. Enter you name @Html.TextBox("Name")
    15. <input type="submit" id="SubmitName" value="Submit"/>
    16. </p>
    17. <p id="rData"></p>
    18. </div>
    Add the Method in Controller that simply return the Message.
    1. public string InputMessage(string input)
    2. {
    3. if (!String.IsNullOrEmpty(input))
    4. return "TextBox Value is =====> " + input + ".";
    5. else
    6. return "Please enter your name.";
    7. }
  2. Using Textbox Filter the data.
    1. <h1>Filter Data With Get Metohod using Json Result</h1>
    2. <p id="rDataT"></p>
    3. <style>
    4. table, th , td {
    5. border: 1px solid grey;
    6. border-collapse: collapse;
    7. padding: 5px;
    8. }
    9. table tr:nth-child(odd) {
    10. background-color: #f1f1f1;
    11. }
    12. table tr:nth-child(even) {
    13. background-color: #ffffff;
    14. }
    15. </style>
    16. <p>
    17. Enter Filter @Html.TextBox("Filter")
    18. <input type="submit" id="GetCustomers" value="Submit"/>
    19. </p>
    20. <script type="text/jscript">
    21. $('#GetCustomers').click(function () {
    22. $.getJSON('../Home/GetStudentList/' + $('#Filter').val(), function (data) {
    23. var items = '
    24. <table>
    25. <tr>
    26. <th>Student Name</th>
    27. <th>Student Admission Number</th>
    28. </tr>';
    29. $.each(data, function (i, item) {
    30. items += "
    31. <tr>
    32. <td>" + item.StdName + "</td>
    33. <td>" + item.AdmNo + "</td>
    34. </tr>";
    35. });
    36. items += "
    37. </table>";
    38. $('#rDataT').html(items);
    39. });
    40. })
    41. </script>
    Also call the json result in controller : On the below i have Linq to SQL.
    1. public JsonResult GetStudentList(string id)
    2. {
    3. DataClasses1DataContext db = new DataClasses1DataContext();
    4. var result = from r in db.StudentMasters
    5. where r.StdName.StartsWith(id)
    6. select new { r.StdName, r.AdmNo };
    7. return Json(result, JsonRequestBehavior.AllowGet);
    8. }
  3. On button Click get the Static Data.
    1. <input id="btnGet" type="button" value="Get Data From List" />
    2. <div>
    3. <div id="ajaxDiv"></div>
    4. </div>
    5. <script type="text/javascript">
    6. $(document).ready(function () {
    7. $('#btnGet').click(function () {
    8. $.getJSON("../Home/GetJsonData", null, function (data) {
    9. var div = $('#ajaxDiv');
    10. div.html("
    11. <br/> " + "Fetch from json Static List: " + "
    12. <br/>");
    13. $.each(data, function (i, item) {
    14. printPerson(div, item);
    15. });
    16. });
    17. });
    18. });
    19. function printPerson(div, item) {
    20. var tbl = "";
    21. div.append("
    22. <br/>" + "FName: " + item.FirstName + ", LName: " + item.LastName);
    23. $.each(item.Addresses, function (i, addr) {
    24. printAddress(div, addr);
    25. });
    26. }
    27. function printAddress(div, item) {
    28. div.append("
    29. <br/>" + " " + "Address: " + item.currentAddress);
    30. }
    31. </script>
    In the controller class looks like:
    1. public JsonResult GetJsonData() {
    2. var persons = new List < Person > {
    3. new Person {
    4. Id = 1, FirstName = "Jayant",
    5. LastName = "Tripathy",
    6. Addresses = new List < Address > {
    7. new Address {
    8. currentAddress = "xyz"
    9. },
    10. }
    11. },
    12. new Person {
    13. Id = 2, FirstName = "Sri",
    14. LastName = "Alex",
    15. Addresses = new List < Address > {
    16. new Address {
    17. currentAddress = "Delhi"
    18. },
    19. }
    20. }
    21. };
    22. return Json(persons, JsonRequestBehavior.AllowGet);
    23. }
    Also add the property classes:
    1. public class Person
    2. {
    3. public int Id { get; set; }
    4. public string FirstName { get; set; }
    5. public string LastName { get; set; }
    6. public List<Address> Addresses { get; set; }
    7. }
    8. public class Address
    9. {
    10. public string currentAddress { get; set; }
    11. }
    The Final code of GET method .cshtml is:
    1. @{
    2. ViewBag.Title = "FormGet";
    3. }
    4. <!DOCTYPE html>
    5. <html>
    6. <head>
    7. <meta name="viewport" content="width=device-width" />
    8. <title>FormGet</title>
    9. <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"
    10. rel="stylesheet" type="text/css"/>
    11. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    12. </head>
    13. <script type="text/javascript">
    14. $(document).ready(function () {
    15. $('#SubmitName').click(function () {
    16. var url = "../Home/InputMessage";
    17. var name = $('#Name').val();
    18. $.get(url, { input: name }, function (data) {
    19. $("#rData").html(data);
    20. });
    21. })
    22. });
    23. </script>
    24. <body>
    25. <div>
    26. <p>
    27. Enter you name @Html.TextBox("Name")
    28. <input type="submit" id="SubmitName" value="Submit"/>
    29. </p>
    30. <p id="rData"></p>
    31. </div>
    32. ------------------------------------------------------------------------------------------------------
    33. <h1>Filter Data With Get Metohod using Json Result</h1>
    34. <p id="rDataT"></p>
    35. <style>
    36. table, th , td {
    37. border: 1px solid grey;
    38. border-collapse: collapse;
    39. padding: 5px;
    40. }
    41. table tr:nth-child(odd) {
    42. background-color: #f1f1f1;
    43. }
    44. table tr:nth-child(even) {
    45. background-color: #ffffff;
    46. }
    47. </style>
    48. <p>
    49. Enter Filter @Html.TextBox("Filter")
    50. <input type="submit" id="GetCustomers" value="Submit"/>
    51. </p>
    52. <script type="text/jscript">
    53. $('#GetCustomers').click(function () {
    54. $.getJSON('../Home/GetStudentList/' + $('#Filter').val(), function (data) {
    55. var items = '
    56. <table>
    57. <tr>
    58. <th>Student Name</th>
    59. <th>Student Admission Number</th>
    60. </tr>';
    61. $.each(data, function (i, item) {
    62. items += "
    63. <tr>
    64. <td>" + item.StdName + "</td>
    65. <td>" + item.AdmNo + "</td>
    66. </tr>";
    67. });
    68. items += "
    69. </table>";
    70. $('#rDataT').html(items);
    71. });
    72. })
    73. </script>
    74. -----------------------------------------------------------
    75. <input id="btnGet" type="button" value="Get Data From List" />
    76. <div>
    77. <div id="ajaxDiv"></div>
    78. </div>
    79. <script type="text/javascript">
    80. $(document).ready(function () {
    81. $('#btnGet').click(function () {
    82. $.getJSON("../Home/GetJsonData", null, function (data) {
    83. var div = $('#ajaxDiv');
    84. div.html("
    85. <br/> " + "Fetch from json Static List: " + "
    86. <br/>");
    87. $.each(data, function (i, item) {
    88. printPerson(div, item);
    89. });
    90. });
    91. });
    92. });
    93. function printPerson(div, item) {
    94. var tbl = "";
    95. div.append("
    96. <br/>" + "FName: " + item.FirstName + ", LName: " + item.LastName);
    97. $.each(item.Addresses, function (i, addr) {
    98. printAddress(div, addr);
    99. });
    100. }
    101. function printAddress(div, item) {
    102. div.append("
    103. <br/>" + " " + "Address: " + item.currentAddress);
    104. }
    105. </script>
    106. </body>
    107. </html>
    And the Controller class Look like:
    1. namespace jQuery_Ajax_GET_POST_MVC.Controllers {
    2. public class HomeController: Controller {
    3. //
    4. // GET: /Home/
    5. public ActionResult FormGet() {
    6. return View();
    7. }
    8. public string InputMessage(string input) {
    9. if (!String.IsNullOrEmpty(input)) return "TextBox Value is =====> " + input + ".";
    10. else return "Please enter your name.";
    11. }
    12. public JsonResult GetStudentList(string id) {
    13. DataClasses1DataContext db = new DataClasses1DataContext();
    14. var result = from r in db.StudentMasters
    15. where r.StdName.StartsWith(id)
    16. select new {
    17. r.StdName, r.AdmNo
    18. };
    19. return Json(result, JsonRequestBehavior.AllowGet);
    20. }
    21. public JsonResult GetJsonData() {
    22. var persons = new List < Person > {
    23. new Person {
    24. Id = 1, FirstName = "Jayant",
    25. LastName = "Tripathy",
    26. Addresses = new List < Address > {
    27. new Address {
    28. currentAddress = "xyz"
    29. },
    30. }
    31. },
    32. new Person {
    33. Id = 2, FirstName = "Sri",
    34. LastName = "Alex",
    35. Addresses = new List < Address > {
    36. new Address {
    37. currentAddress = "Delhi"
    38. },
    39. }
    40. }
    41. };
    42. return Json(persons, JsonRequestBehavior.AllowGet);
    43. }
    44. }
    45. public class Person {
    46. public int Id {
    47. get;
    48. set;
    49. }
    50. public string FirstName {
    51. get;
    52. set;
    53. }
    54. public string LastName {
    55. get;
    56. set;
    57. }
    58. public List < Address > Addresses {
    59. get;
    60. set;
    61. }
    62. }
    63. public class Address {
    64. public string currentAddress {
    65. get;
    66. set;
    67. }
    68. }
    69. }

The output looks like for 3 result is:

code output

POST Method Call

The .cshtml page like below:

  1. @{
  2. ViewBag.Title = "FormPost";
  3. }
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta name="viewport" content="width=device-width" />
  8. <title>FormGet</title>
  9. <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"
  10. rel="stylesheet" type="text/css"/>
  11. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
  12. </head>
  13. <h2>FormPost</h2>
  14. <p>
  15. Enter your First name
  16. <br />
  17. @Html.TextBox("FirstName")
  18. </p>
  19. <p>
  20. Enter your Last name
  21. <br />
  22. @Html.TextBox("LastName")
  23. </p>
  24. <input type="button" value="Save" id="Save" />
  25. <span id="msg" style="color:red;"/>
  26. <script type="text/javascript">
  27. $(document).ready(function () {
  28. $('#Save').click(function () {
  29. var url = "../Home/PostForm";
  30. var FirstName = $("#FirstName").val();
  31. var LastName = $("#LastName").val();
  32. $.post(url, { FirstName: FirstName, LastName: LastName }, function (data) {
  33. $("#msg").html(data);
  34. });
  35. })
  36. });
  37. </script>
  38. </html>
And the Controller class alike:
  1. public ActionResult FormPost() {
  2. return View();
  3. }
  4. [HttpPost]
  5. public string PostForm(string FirstName, string LastName) {
  6. if (!String.IsNullOrEmpty(FirstName) && !String.IsNullOrEmpty(LastName)) {
  7. //You can coding here to save the data from dtabase here
  8. // I just use for return the meassge only !
  9. return "Thank you " + FirstName + " " + LastName + ". Record Saved.";
  10. } else {
  11. return "Please input valid details";
  12. }
  13. }
The Output of Post Method is:

output