asp.net mvc 3 Html.GetUnobtrusiveValidationAttributes 和嵌套模型
情况是这样的。我有一些包含嵌套复杂模型类型的 ViewModel。这是其中一个模型的片段(这不是完整的类):
/// <summary>
/// Defines the overall view when viewing contact details.
/// </summary>
public sealed class ContactDetailsViewModel : BaseViewModel
{
public ContactDetailsViewModel() : base() { }
public ContactDetailsViewModel(WebSession webSession) : base(webSession) { }
public ContactDetailsViewModel(WebSession webSession, string returnUrl) : base(webSession, returnUrl) { }
#region Contact
/// <summary>
/// The contact being viewed.
/// </summary>
public ContactModel Contact { get; set; }
#endregion
以及 ContactModel 类的片段(不完整):
#region Company
[Required(AllowEmptyStrings = false)]
[StringLength(128)]
public string Company { get; set; }
#endregion
我的问题是我需要能够从嵌套模型类型获取验证属性, ContactModel,用于客户端验证。我在大多数视图中使用手动方法,因为我不使用 *For() 辅助方法。这就是我所做的:
@Html.TextBox(
_titleField,
Model.Contact.Title,
new Dictionary<string,object>(Html.GetUnobtrusiveValidationAttributes("Company"))
{
{ "class", "CTextBox" },
{ "style", "width:100%;" }
})
这就像冠军一样,仅当视图的模型是应用了“验证”属性的模型时,才会从模型生成预期的验证属性。具有“验证”属性的模型位于嵌套模型对象内,这是行不通的。我正在努力弄清楚如何获取此场景的验证属性。该视图很复杂,我无法将其进一步分解为模型的多个视图,因此我需要它与嵌套模型一起使用。
我已经尝试了多种方法,但还不会深入研究它们,试图获得纯净的答案。我会说我已经尝试使用我的绑定前缀以及通过一些静态“ModelMetadata”类来钻取我的嵌套模型结构,但到目前为止还没有运气。任何帮助表示赞赏。
让我知道您还需要什么帮助。
我也读过很多布拉德·威尔逊(Brad Wilson)的博客文章以及他领域内的其他人的文章,但到目前为止还没有运气。
Here is the situation. I have some ViewModels that contain nested complex model types. Here is a snippet of one of the models (this is not a complete class):
/// <summary>
/// Defines the overall view when viewing contact details.
/// </summary>
public sealed class ContactDetailsViewModel : BaseViewModel
{
public ContactDetailsViewModel() : base() { }
public ContactDetailsViewModel(WebSession webSession) : base(webSession) { }
public ContactDetailsViewModel(WebSession webSession, string returnUrl) : base(webSession, returnUrl) { }
#region Contact
/// <summary>
/// The contact being viewed.
/// </summary>
public ContactModel Contact { get; set; }
#endregion
And a snippet from the ContactModel class (not complete):
#region Company
[Required(AllowEmptyStrings = false)]
[StringLength(128)]
public string Company { get; set; }
#endregion
My issue is that I need to be able to obtain the validation attributes from the nested model type, ContactModel, for client-side validation. I use the manual approach in most views because I don't use the *For() helper methods. Here is what I do:
@Html.TextBox(
_titleField,
Model.Contact.Title,
new Dictionary<string,object>(Html.GetUnobtrusiveValidationAttributes("Company"))
{
{ "class", "CTextBox" },
{ "style", "width:100%;" }
})
This works like a champ and produces the expected validation attributes from the Model, ONLY, when the Model for the view is the Model that has the "validation" attributes applied. When the Model having the "validation" attributes is within a nested Model object, this does not work. I am struggling trying to figure out how to get the validation attributes for this scenario. The view is complicated, and I cannot decompose it any further into multiple views of models, so I need it working with a nested model.
I have tried numerous approaches, but won't get into them yet in an attempt to get untainted answers. I will say I have tried using my binding prefix as well as going through some of the static "ModelMetadata" classes to drill into my nested model structure, but as of yet no luck. Any help is appreciated.
Let me know what else you need to maybe help.
I have also read a lot of Brad Wilson's blog posts as well as other guys in his realm, but no luck so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须提供属性的完整名称:
其中“Contact”是实例的名称
You must to provide the complete name of the attribute:
Where "Contact" is the name of the instance