我第一次使用基因敲除,而我正在努力解决问题。MVC基因敲除验证显示和
我有一个包含多个部分的页面,希望能够编辑一个部分并提交给控制器,然后显示保存的详细信息。
每个部分是包含显示信息和表单的局部视图。它们根据需要显示和隐藏。我的代码工作提交,但问题是当ModelState无效。我需要返回到显示验证消息的表单
如何在服务器验证失败时再次显示表单?当验证失败时,它当前返回到显示部分。
另外我注意到验证消息不显示。
我相信这肯定是一个简单修复的常见问题。我知道有基因敲除验证工具,但后来需要做更复杂的业务逻辑验证,并需要使这项技术发挥作用。
视图模型:
[Required]
public DateTime? InterviewDate { get; set; }
查看:
<div data-bind="if: showAdminInterviewDisplay" id="Display">
<div>
<button data-bind="click: showInterviewForm" id="EditButton">Edit</button>
</div>
<div>
@Html.Label("Inteview Date") :
<label data-bind="text: interviewDate"></label>
</div>
</div>
<div data-bind="if: showAdminInterviewForm" id="Form">
<div>
@Html.Label("Interview Date")
<input data-bind="value: interviewDate" id="interviewDatePicker" />
@Html.ValidationMessageFor(m => m.InterviewDate)
</div>
<div>
<button data-bind="click: saveInterviewDate">Submit</button>
</div>
淘汰赛视图模型:
function InterviewViewModel() {
//Data
var self = this;
var jsonDate = @Html.Raw(Json.Encode(@Model.InterviewDate));
var date = new Date(parseInt(jsonDate.substr(6)));
self.interviewDate = ko.observable(dateFormat(date, "dd/mm/yyyy"));
self.showAdminInterviewDisplay = ko.observable(true);
self.showAdminInterviewForm = ko.observable();
self.showInterviewForm = function() {
self.showAdminInterviewDisplay(false);
self.showAdminInterviewForm(true);
$("#interviewDatePicker").datepicker({dateFormat: 'dd/mm/yy'});
};
//Operations
self.saveInterviewDate = function() {
$.ajax("@Url.Action("SaveInterview")", {
data: ko.toJSON(self),
type: "post",
contentType: "application/json",
success: function(data) {
self.showAdminInterviewDisplay(true);
self.showAdminInterviewForm(false);
}
});
};
};
ko.applyBindings(new InterviewViewModel());
控制器:
public ActionResult SaveInterview(KnockoutViewModel model)
{
if (ModelState.IsValid)
{
return Json(model);
}
return PartialView("_AdminInterview", model);
}