我正在開發具有Index.cshtml(主要pg,搜索條件爲&搜索按鈕),_PartialView.cshtml(部分視圖顯示帶有更新/刪除鏈接的記錄)的MVC4應用程序,編輯.cshtml(查看更新)。在MVC的同一彈出對話框中顯示模型錯誤4
點擊更新鏈接後,彈出模態對話框打開,其中包含編輯視圖(Edit.cshtml)我申請要求& StringLength屬性的一些字段。如果某些屬性沒有滿足,我想在相同的彈出窗口中顯示它們。但在控制器操作方法中,我正在返回視圖,因此模式彈出消失&編輯視圖在div中打開。
我的記錄顯示,與更新鏈接partialView(_PartialView.cshtml)電網和javascript功能
<a href="javascript:void(0);" class="anchorDetail" onclick="editFunc(@item.SrNo)">Update</a></pre>
function editFunc(id) {
var TeamDetailPostBackURL = '/Home/Edit';
var $buttonClicked = $(this);
var options = { "backdrop": "static", keyboard: true };
$.ajax({
type: "GET",
url: TeamDetailPostBackURL,
cache: false,
contentType: "application/json; charset=utf-8",
data: { "SrNo": id },
datatype: "json",
success: function (data) {
$('#myModalContent').html(data);
$('#myModal').css('display', 'block');
$("#myModal").dialog({
title: 'Edit Data',
modal: true,
bgiframe: true,
show: 'slide',
hide: 'slide',
width: 750
});
$('#myModal').dialog("show");
},
error: function() {
alert("Dynamic content load failed.");
}
});
}
我Edit.cshtml:
@using (Ajax.BeginForm("Edit", "Home", new AjaxOptions { UpdateTargetId = "divPartial", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnBegin = "CallEditBegin", OnSuccess = "CallUnblock" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label" style="display:none" >
@Html.LabelFor(model => model.SrNo)
</div>
<div class="editor-field" style="display:none">
@Html.EditorFor(model => model.SrNo)
@Html.ValidationMessageFor(model => model.SrNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReaderName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReaderName)
@Html.ValidationMessageFor(model => model.ReaderName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UploadDate)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.UploadDate, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.UploadDate)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
我HomeController.cs
[HttpPost]
public ActionResult Edit(UploadData uploadData)
{
if (!ModelState.IsValid)
{
return View(uploadData);
}
else
{
// Processing Logic here
}
}
我的模型類(UploadData.cs)是:
public class UploadData
{
public int SrNo { get; set; }
[Display(Name = "User Name")]
[Required(ErrorMessage = "User Name is required")]
public string UserName { get; set; }
[Display(Name = "Reader Name")]
[Required(ErrorMessage = "Reader Name is required")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string ReaderName { get; set; }
[Required(ErrorMessage = "Upload Date is required")]
[Display(Name = "Upload Date")]
public DateTime UploadDate { get; set; }
}
現在一些提交是必需的,ReaderName具有6個字符的最小長度的恢復。如果任何條件未滿足塊將被執行。
if (!ModelState.IsValid)
{
return View(uploadData);
}
但接下來發生的事情,我彈出的對話框消失&格將上顯示的錯誤消息主要PG開放。 我想在彈出窗口中將它們顯示在控件之前。
我很困惑在模型錯誤的情況下該怎麼做,以便它們在控制之後出現在相同的彈出窗口中。 請給些建議,我錯過了什麼.....
不明白你正在試圖解釋什麼。請參閱更新的問題以獲取更多詳細信息。 –