mvc clientside validation for nested (collection) properties

前端 未结 1 947
长情又很酷
长情又很酷 2020-12-10 07:35

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

My primary reas

相关标签:
1条回答
  • 2020-12-10 08:06

    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

    0 讨论(0)
提交回复
热议问题