asp.net mvc 2 - How can I validate using DataAnnotations only part of fields in Viewmodel? -
i've got viewmodel looks this:
public class viewmodel { public int type {get;set} // 0 if typea, 1 if typeb [required] public string aproperty1 {get;set} [required] public string aproperty1 {get;set} ... [required] public string bproperty1 {get;set} [required] public string bproperty1 {get;set} } there 2 forms viewmodel , in forma user inputs aproperty1, aproperty2 etc. , bproperty-s return null. same formb. type of form (forma or formb) assigned viewmodel.type field.
so problem in controller check modelstate.isvalid property , it'll false in both ways because half of fields null.
one possible solution somehow override modelstate.isvalid property in modelview pass type it. far know there no way to.
are there other solutions? (it preferrably should able use client-side validation)
if requirement have 1 viewmodel 2 different views, "partial validation" solution. check out blog:
http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/
Comments
Post a Comment