我在頁面上方的結果表中有一個小的部分Create Person表單。我希望能夠將表單發佈到服務器,這對於ajax.Beginform沒有任何問題。MVC 2 Ajax.Beginform通過返回的HTML + JSON到JavaScript函數
<% using (Ajax.BeginForm("Create", new AjaxOptions { OnComplete = "ProcessResponse" }))
{%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%=Html.LabelFor(model => model.FirstName)%>
</div>
<div class="editor-field">
<%=Html.TextBoxFor(model => model.FirstName)%>
<%=Html.ValidationMessageFor(model => model.FirstName)%>
</div>
<div class="editor-label">
<%=Html.LabelFor(model => model.LastName)%>
</div>
<div class="editor-field">
<%=Html.TextBoxFor(model => model.LastName)%>
<%=Html.ValidationMessageFor(model => model.LastName)%>
</div>
<p>
<input type="submit" />
</p>
</fieldset>
<%
}
%>
然後在我的控制器,我希望能夠回來後部分這僅僅是一個錶行 如果創建成功,並且把它添加到表,我可以用jQuery輕鬆做到。
$('#personTable tr:last').after(data);
但是,如果服務器驗證失敗我想通過我的後面部分創建人形式與驗證錯誤,並取代現有的創建人形式。
我試圖返回JSON數組
控制器:
return Json(new
{
Success = true,
Html= this.RenderViewToString("PersonSubform",person)
});
使用Javascript:
var json_data = response.get_response().get_object();
與合格/不合格標誌,並使用下面的孤子呈現爲字符串部分但是當表單失敗時不會呈現mvc驗證控件。
那麼,有沒有什麼辦法可以手寫我的開箱PartialView(「爲personForm」)的JavaScript作爲從我ajax.form它回來了?我可以傳遞一些附加信息作爲Json數組,以便我可以判斷它的通過或失敗,並可能添加消息?
UPDATE
我現在可以通過一個PartialView我的JavaScript的HTML,但我需要通過像ServerValidation一些額外的數據對:真/假和ActionMessage的:「你剛纔創建的比爾的人」。理想情況下,我會傳遞Json數組而不是隱藏字段。
function ProcessResponse(response) {
var html = response.get_data();
$("#campaignSubform").html(html);
}
提前感謝