Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. public class ClientModelData
  2. {
  3. public int ClientID { get; set; }
  4. public string ClientName { get; set; }
  5. public string Phone { get; set; }
  6. public string Address { get; set; }
  7. public IList<int> LocationID { get; set; }
  8. }
  9.  
  10. [HttpPost]
  11. public ActionResult AddClient(ClientModelData client)
  12. {
  13. Client _client = new Client()
  14. {
  15. //ClientCode=client.ClientCode,
  16. ClientName = client.ClientName,
  17. Phone = client.Phone,
  18. Ext = client.Ext,
  19. Address = client.Address,
  20. CreatedBy = 1,
  21. CreatedDate = DateTime.Now,
  22. ModifiedBy = 1,
  23. ModifiedDate = DateTime.Now,
  24. ModifiedIP = Request.ServerVariables["REMOTE_ADDR"]
  25. };
  26. db.Clients.Add(_client);
  27. try
  28. {
  29. db.SaveChanges();
  30. }
  31. catch (DbEntityValidationException dbEx)
  32. {
  33. string error = string.Empty;
  34. foreach (var validationErrors in dbEx.EntityValidationErrors)
  35. foreach (var validationError in validationErrors.ValidationErrors)
  36. error += string.Format("Property: {0} Error: {1}", validationError.PropertyName,
  37. validationError.ErrorMessage);
  38.  
  39. return Json(new
  40. {
  41. status = false,
  42. message = "Unable to update information for selected client, please contact administrator. For detail <a class='errordetail'>click here</a>",
  43. exception = error
  44. });
  45. }
  46. return Json(_client);
  47. }
  48.  
  49. <ul class="locations">
  50. @foreach (var item in Model.Locations)
  51. {
  52. <li>
  53. <label>
  54. <input type="checkbox" id="chkLocations" name="chkLocations"
  55. value="@item.LocationID" />
  56. @item.LocationTitle</label>
  57. </li>
  58. }
  59. </ul>
  60.  
  61. var allVals = [];
  62. $('#chkLocations:checked').each(function () {
  63. allVals.push($(this).val());
  64. });
  65. var dataPacket = {
  66. ClientName: $("#txtClientName").val(),
  67. Phone: $("#txtPhone").val(),
  68. Ext: $("#txtExt").val(),
  69. Address: $("#txtAddress").val(),
  70. LocationID: allVals
  71. };
  72.  
  73. var allVals = [];
  74. $('#chkLocations:checked').each(function () {
  75. allVals.push($(this).val());
  76. });
  77. var dataPacket = {
  78. ClientName: $("#txtClientName").val(),
  79. Phone: $("#txtPhone").val(),
  80. Ext: $("#txtExt").val(),
  81. Address: $("#txtAddress").val(),
  82. LocationID: allVals
  83. };
  84.  
  85. $.ajax({
  86. url: '@Url.Action("AddClient", "SomeController")',
  87. type: 'POST',
  88. contentType: 'application/json',
  89. data: JSON.stringify({ client: dataPacket }),
  90. success: function(result) {
  91. // ... handle the result
  92. }
  93. });
Add Comment
Please, Sign In to add comment