【发布时间】:2014-05-27 12:45:32
【问题描述】:
我正在将创建表单更改为模式对话框,并且 jquery 不显眼的验证停止工作并且不知道如何修复它。
Index.cshtml 有一个链接可以触发一个模式对话框。
<a href="#" id="createCustomer">Create</a>
@section scripts{
<script type="text/javascript">
$('#createCustomer').on('click', function () {
$.get('/Customer/Create', function (data) {
$('#modalContainer').html(data);
$('#myModal').modal({});
});
});
Create.cshtml 是一个局部视图。
@model Demo.Web.Models.CustomerVm
@using (Html.BeginForm("Create", "Customer", FormMethod.Post, new { @id="createForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
控制器上有一个actionmethod,它返回创建表单的部分视图。
public ActionResult Create()
{
return PartialView("Create");
}
查看模态
public class CustomerVm
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
在我将其更改为模态对话框之前 .. 一切正常,但现在我不知道如何修复它。如何使验证与对话框一起使用?显然,我不想重写客户端脚本上的验证规则..我想让它从视图模型中工作..谢谢。
【问题讨论】:
标签: asp.net-mvc data-annotations unobtrusive-validation asp.net-mvc-viewmodel