我打電話從阿賈克斯的方法,通過加載了一個局部視圖&適當地設定包含的屬性更新頁面中的一些內容。在局部視圖返回的ValidationSummary阿賈克斯錯誤處理 - 沒有別的越來越呈現
這一切工作正常,但我在使用ValidationSummary控件服務器端驗證的問題。我在其他地方使用了一種方法來將ValidationSummary貼在部分視圖中。 (我實際上已關閉客戶端驗證&我試圖讓服務器端驗證使用與客戶端驗證使用相同的驗證摘要工作)
當出現驗證錯誤時,應該更新ajax錯誤處理程序div的內容與返回的部分視圖。
所有這些工作正常,即驗證摘要使用期望的錯誤消息進行呈現,除了除了包含驗證摘要及其包含元素的局部視圖以外的任何其他內容不顯示在頁面上。即沒有的div /兄弟姐妹越來越呈現
我reaslly沒有得到一個線索是怎麼回事。如果我在錯誤處理程序中註釋掉部分更新div的行,我會再次看到整個表單。
我使用ExceptionFilter:
public class ValidationErrorAttribute : FilterAttribute, IExceptionFilter
{
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.Exception != null)
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.HttpContext.Response.StatusCode =
(int)System.Net.HttpStatusCode.BadRequest;
//return the partial view containing the validationsummary and set the ViewData
//so that it displays the validation error messages
filterContext.Result = new PartialViewResult { ViewName = "validation",
ViewData = filterContext.Controller.ViewData };
}
}
}
時,有一個驗證錯誤,扔掉validationexception將 觸發過濾器的onException的方法
//Controller
[HttpPost]
[ValidationErrorAttribute]
public ActionResult Save(ConsumptionData consumptionData)
{
if (ModelState.IsValid)
{
var dto = Mapper.Map<ConsumptionData, ConsumptionDataDto>(consumptionData);
var refId = _personalProjectionRepository.AddPersonalProjectionRecord(dto);
consumptionData.ReferenceId = refId;
return PartialView("Reference", consumptionData);
}
else
{
throw new ValidationException();
}
}
驗證局部視圖:
@model PersonalProjection.Web.Models.ConsumptionData
@Html.ValidationSummary()
和jQuery的:
$('form').submit(function() {
var form = $(this);
if (form.valid())
{
$.ajax({
url: form.attr("action"),
type: "POST",
dataType: "html",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
SiteId: $('#siteid').val(),
ElectricityConsumption: $('#electricityconsumption').val(),
ElectricitySpend: $('#electricityspend').val(),
GasConsumption: $('#gasconsumption').val(),
GasSpend: $('#gasspend').val()
}),
success: function (result) {
$('#rightPanelSection').html(result);
},
error: function (jqXHR) {
$('#validationSummary').html(jqXHR.responseText);
}
});
}
return false;
});
和index.cshtml標記
<div class="panel panel-outcome">
@using (Html.BeginForm("Save", "Home", FormMethod.Post, (object)new { ID =
"projectionForm" }))
{
<div id="validationSummary"/>
<div id="topPanelSection" class="panel-section">
@Html.LabelFor(m => m.SiteId, new { id = "siteidlabel" })
@Html.TextBoxFor(m => m.SiteId, ViewBag.TextBoxDisabled ? (object)new { id =
"siteid", disabled = true, maxlength = 9, @class = "input-disabled", @Value =
ViewBag.SiteId } : new { id = "siteid", maxlength = 9 })
<span id="errorText" class="error-text" />
</div>
<div id="leftPanelSection" class="panel-section">
<div class="column-heading">
<span class="column-heading-left">Total Cost</span>
<span class="column-heading-right">Total Consumption</span>
</div>
<div class="panel-section-row">
@Html.LabelFor(m => m.ElectricitySpend, new { id = "electricitylabel" })
<span>£</span>
@Html.TextBoxFor(m => m.ElectricitySpend, new { @class = "textbox-right-
margin", id = "electricityspend" })
@Html.TextBoxFor(m => m.ElectricityConsumption, new { @class = "textbox-
left-margin", id = "electricityconsumption" })
<span>kWhs</span>
</div>
<div class="panel-section-row">
@Html.LabelFor(m => m.GasSpend, new { id = "gaslabel" })
<span>£</span>
@Html.TextBoxFor(m => m.GasSpend, new { @class = "textbox-right-margin", id =
"gasspend" })
@Html.TextBoxFor(m => m.GasConsumption, new { @class = "textbox-left-margin",
id = "gasconsumption" })
<span>kWhs</span>
</div>
<div class="panel-section-row">
<button type="reset" id="resetbutton">Reset Form</button>
<button type="submit">Generate Reference Id</button>
</div>
</div>
<div id="rightPanelSection" class="panel-section">
@Html.Partial("Reference", Model)
</div>
}