我很难让客户端验证在部分 View 中的模式弹出窗口中工作。我使用按钮在主 .cshtml 文件中调用它。
<div>
@Html.ActionLink("Add", "_MyPartial", "MyController", new { clientID = "0" }, new {@id = "btnAdd", })
</div>
部分 View 加载模态弹出窗口非常好,我可以对它进行 CRUD。看起来像这样。
@using(Html.BeginForm("_MyPartial", "MyController", FormMethod.Post, new { id = "frmClient" }))
{
<div class="modal-content panel panel-info">
<div class="modal-header panel-heading">
<button type = "button" class="close" data-dismiss="modal">×</button>
</div>
<div class="panel-body bootstrap-padding-overide">
@Html.ValidationSummary(false, null, new { @id = "ValidationSummary", @class = "validationErrorBox" })
</div>
<div class="modal-body panel-body bootstrap-padding-overide">
<div>
@Html.LabelFor(m => m.selectName)
@Html.TextBoxFor(m => m.selectName, new { @class = "form-control" })
</div>
<div>
<input type = "submit" name="submitButton" value="Submit" class="btn btn-default" />
</div>
</div>
</div>
}
@section MyScripts
{
<script type="text/javascript">
$.validator.unobtrusive.parse("#frmClient form");
</script>
}
所以我在布局页面中加载了所有可用的 .js 文件,并在我的 ViewModel 中加载了一个必填字段。
[Required(ErrorMessage = "Please Insert a Name")]
[Display(Name = "Persons Name")]
public string selectName { get; set; }
弹出容器在 layout.cshtml 以及处理模态的 Jquery...
所以当我提交它时,它只是发布到 Controller 操作。我想知道是否有人可以建议我哪里出了问题。
* 已解决* 下面的海报在这里对我帮助很大。由于缺乏理解,我做了很多按钮抨击。所以我想发表评论来解决我在哪里工作。
我将以下内容放入 html.BeginForm 辅助方法的部分页面中。所以在jQuery document.ready函数之前。请指正,我会修改。
@using (Html.BeginForm("_MyPartial", "MyControlloer", FormMethod.Post, new { id = "frmClient" }))
{
<script type="text/javascript">
$('form#frmClient').removeData("validator");
$('form#frmClient').removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse($('form#frmClient'));
</script>
}
请您参考如下方法:
由于加载了部分 View ,因此您必须像下面提到的那样重新添加表单验证
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse($('form'));
将这些行添加到您的局部 View 加载处理程序或操作链接处理程序中。
