asp.net-mvc 不显眼的验证不适用于动态添加的局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9321040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unobtrusive validation not working on dynamically-added partial view
提问by noobi
I am currently facing a problem with validation after dynamically adding content.
我目前在动态添加内容后面临验证问题。
I have a view strongly typed to a model (Order). This Order can have many items. The model looks something like the following:
我有一个强类型到模型 ( Order)的视图。这个订单可以有很多项目。该模型如下所示:
public class Order
{
[Key]
[HiddenInput]
public int id { get; set; }
[Display(Name = "Order Number")]
public string number { get; set; }
[Display(Name = "Order Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime date { get; set; }
[Required(ErrorMessage = "Beneficiary is required.")]
[Display(Name = "Beneficiary")]
public int beneficiary_id { get; set; }
[Display(Name = "Beneficiary")]
public Beneficiary beneficiary { get; set; }
[Display(Name = "Items")]
public List<Item> items { get; set; }
[Display(Name = "Payment Method")]
public List<PaymentMethod> payment_methods { get; set; }
}
I enter the order information and also the items for that specific order. I tried a couple of ways to add content dynamically and finally went with Steven Sanderson's way.
我输入订单信息以及该特定订单的项目。我尝试了几种动态添加内容的方法,最后采用了Steven Sanderson 的方法。
In my view, I have the regular Order information and then the items, where my model looks something like this:
在我看来,我有常规的订单信息,然后是项目,我的模型看起来像这样:
@model trackmeMvc.Models.Model.Order
@{
ViewBag.Title = "Create";
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>
@using (Html.BeginForm("Create", "Order", FormMethod.Post, new { @id = "create_order" }))
{
@Html.ValidationSummary(true, "Order creation was unsuccessful. Please correct the errors and try again.")
<div class="editor-label">
@Html.LabelFor(m => m.date)<req>*</req>
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.date, new { @id = "order_date" })<br />
@Html.ValidationMessageFor(m => m.date)
</div>
...
...
<script type="text/javascript">
$(document).ready(function () {
$("#addItem").click(function () {
var formData = $("#main_div").closest("form").serializeArray();
$.ajax({
url: "/IPO/BlankItemRow",
type: "POST",
//data: formData,
cache: false,
success: function (html) {
$("#editorRows").append(html);
//$.validator.uobtrusive.parseDynamicContent("form *");
//$("#editorRows").removeData("validator");
//$("#editorRows").removeData("unobtrusiveValidation");
//$.validator.unobtrusive.parse("#editorRows");
//$.validator.unobtrusive.parse("#create_ipo");
//$.validator.unobtrusive.parseDynamicContent($(this).first().closest("form"));
//$.validator.unobtrusive.parse($("#new_ipo_item"));
//$.validator.unobtrusive.parseElement($("#editorRows").find(".editRow:last").children().find("select"));
//$("#editorRows").find(".editRow:last").find("select").each(function () {
//alert($(this).attr("id"));
//$.validator.unobtrusive.parseElement($(this));
//$.validator.unobtrusive.parseDynamicContent($(this));
//$.validator.unobtrusive.parseDynamicContent($(this).attr("name"));
//});
//$("#editorRows").children().find(".editRows:last").find("*").each(function () {
// alert($(this).attr('id'));
//$.validator.unobtrusive.parseDynamicContent('input');
//});
//var form = $(this).closest("form").attr("id");
//$(form).removeData("validator");
//$(form).removeData("unobtrusiveValidation");
//$.validator.unobtrusive.parse(form);
}
});
return false;
});
});
</script>
Those are some of the things I tried, and nothing works.
这些是我尝试过的一些东西,但没有任何效果。
I got the parseDynamicContentfrom Applying unobtrusive jquery validation to dynamic content in ASP.Net MVC. I tried it in every scenario I could think of, but still no luck.
我parseDynamicContent从Applying unobtrusive jquery validation to dynamic content in ASP.Net MVC 得到了。我在我能想到的每一种情况下都尝试了它,但仍然没有运气。
I also tried the regular parse, removing validation from the form then applying it again, but still the newly added elements are not validated:
我还尝试了常规解析,从表单中删除验证然后再次应用它,但仍然没有验证新添加的元素:
<div id="editorRows">
@foreach (var item in Model.items)
{
@Html.Partial("_NewItem", item)
}
</div>
... and my partial view would look something like this:
......我的部分观点看起来像这样:
@model trackmeMvc.Models.Model.Item
@{
Layout = "";
Html.EnableClientValidation(true);
if (this.ViewContext.FormContext == null)
{
this.ViewContext.FormContext = new FormContext();
}
}
<div class="editRow">
@using (Html.BeginCollectionItem("order_items"))
{
@Html.DropDownListFor(m => m.item_id, @items, "None", new { @style = "width:205px;", @id = "ddlItems", @class="ddlItem", @name="ddlItemList" })
@Html.ValidationMessageFor(m => m.item_id)
...
}
</div>
So what's happening is, I have one empty item sent from the controller to the view by default, to show one empty row. That item is validated, but whatever comes after when I click add item, another row appears, from that partial, but I can't get it to validate. I tried to put the validation in the partial view, (before the document ready in the main form), and everything I read I applied, and it always ends up the same: validating the first row, and not the others. I tried the validation of Steven Sanderson done for that purpose - still no luck - even the validation for partials, found at this linkand the page that follows which is specific to partial validation...
所以发生的事情是,默认情况下,我有一个从控制器发送到视图的空项目,以显示一个空行。该项目已验证,但是当我单击添加项目后,会出现另一行,来自该部分,但我无法对其进行验证。我试图将验证放在部分视图中(在主表单中准备好文档之前),我阅读的所有内容都应用了,结果总是相同:验证第一行,而不是其他行。我尝试了为此目的对史蒂文桑德森进行的验证 - 仍然没有运气 - 甚至是在此链接和随后特定于部分验证的页面中找到的部分验证......
What should I do to get this validation working?
我应该怎么做才能使此验证工作?
回答by danludwig
Ok, I am going to start over with a new answer here.
好的,我将在这里重新开始一个新的答案。
Before you call $.validator.unobtrusive.parse, remove the original validator and unobtrusive validation from the form like so:
在调用之前$.validator.unobtrusive.parse,从表单中删除原始验证器和不显眼的验证,如下所示:
var form = $("#main_div").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
This same answer is documented here.
回答by devlin carnate
What worked for me was to re-apply the validator after the call to load the partial view. In my case, I'm using $.post().then()but you could do something similar with a .always()callback of an AJAX call.
对我有用的是在调用加载局部视图后重新应用验证器。就我而言,我正在使用,$.post().then()但您可以使用.always()AJAX 调用的回调执行类似的操作。
$.post(url, model, function (data) {
//load the partial view
$("#Partial").html(data);
}).then(function () {
$("form").each(function () { $.data($(this)[0], 'validator', false); });
$.validator.unobtrusive.parse("form");
});

