当用户单击名为 add New 的按钮时,将显示一个名为 AddNew 的弹出窗口。按钮本身具有称为 TRN 和 DOB 的字段数。我现在遇到的问题是,当用户尝试添加重复的 TRN 时,我试图显示错误消息,但不幸的是,只有在必填字段为空时才显示错误消息。
无论如何我可以在验证摘要中显示重复的错误消息,例如在必填字段中显示并在表单中保存所有先前输入的值。请指教。谢谢
这是我到目前为止的代码。谢谢
索引.cshtml
<input type="button" id="btnAddNew" style="height:50px; font-size:14px; width:95px; background-color:#3399FF; white-space: normal;" class="k-button" title="AddNew" value="Add New" />
<script type="text/javascript">
$(document).ready(function () {
$('#btnAddNew').click(function () {
window.open('@Url.Action("AddNew", "Details")', 'AddProduct', 'height=' + (window.screen.height - 450) + ',width=950,left=' + (window.screen.width - 5) + ',top=10,status=no,toolbar=no,resizable=yes,scrollbars=yes');
});
});
</script>
AddNew.cshtml(弹出窗口)
@{
Layout = "~/Views/Shared/_LayoutNoMenu.cshtml";
}
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
@model HHIMS_Web_App.Models.CModel
@{
ViewBag.Title = "New Product";
}
@using (Html.BeginForm("Create", "Details", FormMethod.Post, new { id = "frmAsset" }))
{
ViewContext.FormContext.ValidationSummaryId = "valSumId";
@Html.ValidationSummary(false, "Please fix the following errors:", new Dictionary<string, object> { { "id", "valSumId" } });
<fieldset id="AddNew">
<div>
<div class="addInfo">
@Html.LabelFor(model => model.TRN)
@Html.EditorFor(model => model.TRN)
@Html.ValidationMessageFor(model => model.TRN, "*")
</div>
<div class="AddSmallBox">
@Html.LabelFor(model => model.DOB)
@Html.EditorFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB, "*")
</div>
</div>
<div>
<div class="smallAddAndCancel">
<input type="button" id="btnCancel" style="height:50px; width:85px; font-size:14px; background-color:#3399FF" class="k-button" title="Cancel" value="Cancel" onclick="window.close()" />
<input type="submit" id="btnSave" style="height:50px; width:85px; font-size:14px; background-color:#3399FF;white-space: normal" class="k-button" title="Save" value="Save"/>
</div>
</div>
</fieldset>
}
<script type="text/javascript">
$(document).ready(function () {
$("#datepicker").closest("span.k-datepicker").width(400);
$('#btnSave').click(function () {
var validation = $("#frmAsset");
if (!validation.valid()) {
return false;
else
{
$.ajax({
type: 'POST',
url: "@Url.Action("Create","Details")",
data: {
TRN: $("#TRN").val(),
DOB: $("#DOB").val(),
},
success: function () {
window.close()
}
});
}
});
});
</script>
控制器: [HttpPost]
public ActionResult Create(Cmodel)
{
try
{
if (ModelState.IsValid)
{
CheckDuplicateHRNExist(model);
return RedirectToAction("AddNew");
}
if (ModelState.IsValid)
{
HH_DataAccessLayer.Consumers dalModel = new HH_DataAccessLayer.Consumers();
Mapper.CreateMap<CModel, HH_DataAccessLayer.Consumers>();
Mapper.Map(model, dalModel);
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException e)
{
var entry = e.Entries.Single();
var clientValues = (CModel)entry.Entity;
}
return RedirectToAction("Index");
}
/// <summary>
/// Validate TRN find if no duplicate TRN recorded
/// </summary>
/// <param name="model"></param>
private void CheckDuplicateTRNExist(CModel model)
{
HEntities context = new HEntities();
if (model.TRN != null)
{
var duplicateTRN = context.Consumers.Where(d => d.TRN == model.TRN).FirstOrDefault();
if (duplicateTRN != null)
{
var errorMessage = String.Format("TRN is already exist.", duplicateTRN);
ModelState.AddModelError(string.Empty, errorMessage);
}
}
}
模型
[Required(ErrorMessage = "Please enter TRN")]
[DisplayName("TRN")]
[StringLength(20)]
public string TRN { get; set; }
[Required(ErrorMessage = "Please enter or select Date of Birth")]
[DisplayName("Date Of Birth")]
public DateTime? DOB { get; set; }