Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. public class ValidationErrorAttribute : FilterAttribute, IExceptionFilter
  2. {
  3. public virtual void OnException(ExceptionContext filterContext)
  4. {
  5. if (filterContext == null)
  6. {
  7. throw new ArgumentNullException("filterContext");
  8. }
  9.  
  10. if (filterContext.Exception != null)
  11. {
  12. filterContext.ExceptionHandled = true;
  13. filterContext.HttpContext.Response.Clear();
  14. filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
  15. filterContext.HttpContext.Response.StatusCode =
  16. (int)System.Net.HttpStatusCode.BadRequest;
  17. //return the partial view containing the validationsummary and set the ViewData
  18. //so that it displays the validation error messages
  19. filterContext.Result = new PartialViewResult { ViewName = "validation",
  20. ViewData = filterContext.Controller.ViewData };
  21. }
  22. }
  23. }
  24.  
  25. //Controller
  26. [HttpPost]
  27. [ValidationErrorAttribute]
  28. public ActionResult Save(ConsumptionData consumptionData)
  29. {
  30. if (ModelState.IsValid)
  31. {
  32. var dto = Mapper.Map<ConsumptionData, ConsumptionDataDto>(consumptionData);
  33. var refId = _personalProjectionRepository.AddPersonalProjectionRecord(dto);
  34. consumptionData.ReferenceId = refId;
  35. return PartialView("Reference", consumptionData);
  36. }
  37. else
  38. {
  39. throw new ValidationException();
  40. }
  41. }
  42.  
  43. @model PersonalProjection.Web.Models.ConsumptionData
  44. @Html.ValidationSummary()
  45.  
  46. $('form').submit(function () {
  47. var form = $(this);
  48. if (form.valid())
  49. {
  50.  
  51. $.ajax({
  52. url: form.attr("action"),
  53. type: "POST",
  54. dataType: "html",
  55. contentType: "application/json; charset=utf-8",
  56. data: JSON.stringify({
  57. SiteId: $('#siteid').val(),
  58. ElectricityConsumption: $('#electricityconsumption').val(),
  59. ElectricitySpend: $('#electricityspend').val(),
  60. GasConsumption: $('#gasconsumption').val(),
  61. GasSpend: $('#gasspend').val()
  62. }),
  63. success: function (result) {
  64. $('#rightPanelSection').html(result);
  65. },
  66. error: function (jqXHR) {
  67. $('#validationSummary').html(jqXHR.responseText);
  68. }
  69.  
  70. });
  71. }
  72. return false;
  73. });
  74.  
  75. <div class="panel panel-outcome">
  76.  
  77.  
  78. @using (Html.BeginForm("Save", "Home", FormMethod.Post, (object)new { ID =
  79. "projectionForm" }))
  80.  
  81. {
  82. <div id="validationSummary"/>
  83.  
  84. <div id="topPanelSection" class="panel-section">
  85. @Html.LabelFor(m => m.SiteId, new { id = "siteidlabel" })
  86. @Html.TextBoxFor(m => m.SiteId, ViewBag.TextBoxDisabled ? (object)new { id =
  87. "siteid", disabled = true, maxlength = 9, @class = "input-disabled", @Value =
  88. ViewBag.SiteId } : new { id = "siteid", maxlength = 9 })
  89. <span id="errorText" class="error-text" />
  90. </div>
  91. <div id="leftPanelSection" class="panel-section">
  92. <div class="column-heading">
  93. <span class="column-heading-left">Total Cost</span>
  94. <span class="column-heading-right">Total Consumption</span>
  95. </div>
  96. <div class="panel-section-row">
  97. @Html.LabelFor(m => m.ElectricitySpend, new { id = "electricitylabel" })
  98. <span>£</span>
  99. @Html.TextBoxFor(m => m.ElectricitySpend, new { @class = "textbox-right-
  100. margin", id = "electricityspend" })
  101. @Html.TextBoxFor(m => m.ElectricityConsumption, new { @class = "textbox-
  102. left-margin", id = "electricityconsumption" })
  103. <span>kWhs</span>
  104. </div>
  105. <div class="panel-section-row">
  106. @Html.LabelFor(m => m.GasSpend, new { id = "gaslabel" })
  107. <span>£</span>
  108. @Html.TextBoxFor(m => m.GasSpend, new { @class = "textbox-right-margin", id =
  109. "gasspend" })
  110. @Html.TextBoxFor(m => m.GasConsumption, new { @class = "textbox-left-margin",
  111. id = "gasconsumption" })
  112. <span>kWhs</span>
  113. </div>
  114. <div class="panel-section-row">
  115. <button type="reset" id="resetbutton">Reset Form</button>
  116. <button type="submit">Generate Reference Id</button>
  117. </div>
  118. </div>
  119. <div id="rightPanelSection" class="panel-section">
  120. @Html.Partial("Reference", Model)
  121. </div>
  122.  
  123. $('#validationSummary').html(jqXHR.responseText);
  124.  
  125. <div id="validationSummary"/>
  126.  
  127. <div id="validationSummary">
  128.  
  129. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement