如何在部分视图中不使用submit
标记从部分视图触发Html.BeginForm
按钮。原因是当有两个表单标签时,即在主视图和部分视图中,验证不起作用。
重要的是我希望验证有效。
我的部分查看代码:
<div id="AddMe">
<div class="row">
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName) // this should work
</div>
</div>
<div>
<input name="submit" type="submit" id="submit" value="Save" /> // how do i trigger this from partial view
</div>
我的主视图代码:
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
<div class="modal" id="modalId" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body" id="modalbodyId">
@Html.Partial("PartilViewName")
</div>
<div class="modal-header">
<button type="submit">
</div>
</div>
</div>
</div>
}
For Validation purpose, I have added the following lines of code in Main View.
$("#modalId").click(function () {
var form = $("#modalbodyId").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
$.ajax({
url: "/ControllerName/ActionName",
type: "POST",
data: $("#modalbodyId").serialize(),
cache: false
});
});
此外,只有记录所有模型和其他注释都已到位。
注意:我是否需要在Partial View中添加
unobtrusive/ajax
代码。