嵌套(集合)的属性MVC客户方的验证 [英] mvc clientside validation for nested (collection) properties

查看:107
本文介绍了嵌套(集合)的属性MVC客户方的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net MVC 3使用jQuery不显眼的验证。笔者近日从标准DataAnnotations到FluentValidation改变,它的伟大工程。

I'm using asp.net mvc 3 with jquery unobtrusive validation. I recently changed from standard DataAnnotations to FluentValidation and it works great.

我对拾起FluentValidation主要原因是在我的视图模型验证嵌套属性的需要(但我发现有使用它的其他很酷的原因),这有点儿看起来像这样(不介意访问,这是伪):

My primary reason for picking up FluentValidation was the need to validate nested properties on my viewmodel (but i found there are other cool reasons for using it) that kinda looks like this (don't mind accessors this is pseudo):

class Vm {
  string Prop;
  string AnotherProp;
  IEnumerable<ElementsVm> Elements;
}

class ElementsVm {
  bool Required;
  string Id;
  string Title;
  string Value;
}

使用FluentValidation我做出VM和ElementVm的验证和我的单元测试是绿色的,显示我的服务器端验证工作。

Using FluentValidation I make a validator for Vm and for ElementVm and my unit tests are green, showing me server side validation is working.

客户端,'道具'和'AnotherProp工作 - 我的验证规则也运行客户端的预期(因为他们会与DataAnnontation),但我所有的元素都没有得到任何客户端验证在所有 - 我检查DOM和可以看到所有的数据-VAL,数据需要等属性丢失。

Client side, 'Prop' and 'AnotherProp' is working - my validation rules are also running client-side as expected (as they would with DataAnnontation), but all my Elements are not getting any client-side validation at all - i inspect the dom and can see all the data-val, data-required etc. attributes are missing.

我试过不同的方法来产生在我的意见的HTML,但'道具'和'AnotherProp使用Html.TextBoxFor生成(M => m.Prop),而我的元素在部分产生的 - 这是哪里的问题开始。如果让我选择Html.TextBoxFor(M => m.Value)我所有的元素文本框将具有相同的名称/ ID,所以我也使用Html.TextBox(Model.Id)​​生成唯一的ID /名称,但仍然没有验证属性尝试

I've tried different approaches to generating the html in my views, but the 'Prop' and 'AnotherProp' are generated using Html.TextBoxFor(m => m.Prop) while my elements are generated in a partial - this is where the problems start. If i choose Html.TextBoxFor(m => m.Value) all my Element Textboxes will have the same name/id, so i also tried using Html.TextBox(Model.Id) to generate unique id/name but still no validation properties.

那么,有没有办法让我的塞纳里奥的工作​​ - 我不介意改写了一点,但我真的想FluentValidation写我的HTML我

So is there a way to make my senario work - i don't mind rewriting it a bit, but i would really like FluentValidation to write my html for me.

我的备用解决方案是使我自己的HTML辅助生成正确的HTML与属性,但会吸吮我想,既然我将不得不继续更新这些佣工当新版本/补丁要么FluentValidation,jQuery的发验证或两者MVC中的链接。

My fallback solution would be to make my own Html helpers to generate the correct Html with attributes, but that would suck i think, since i would have to keep updating those helpers when new releases/patches were made to either FluentValidation, jquery validation or the link in mvc between the two.

推荐答案

在你的部分,ElementsVM的每个实例之前,必须使用ViewData.TemplateInfo.HtmlField preFIX,像这样设置一个唯一的preFIX:

In your partial, before each instance of ElementsVM, you must set a unique prefix using ViewData.TemplateInfo.HtmlFieldPrefix, like so:

var i = 0; 
foreach (var element in Model) 
{ 
    ViewData.TemplateInfo.HtmlFieldPrefix = "Elements[" + i.ToString() + "]"; 
    @Html.TextBoxFor(m => m.Value) 
    i++; 
}

这应该给你不引人注目的验证属性,也应使用默认模型联工作。

This should give you your unobtrusive validation attributes, and should also work with the default model binder.

counsellorben

counsellorben

这篇关于嵌套(集合)的属性MVC客户方的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆