3

I've got a Viewmodel that looks like 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 are 2 forms that get this viewmodel and in FormA user inputs AProperty1, AProperty2 etc. and BProperty-s return as null. The same with FormB. The type of form (FormA or FormB) is assigned to the ViewModel.type field.

So the problem is that in my controller I check the ModelState.IsValid property and it'll be false in both ways because half of the fields are always null.

One possible solution could be to somehow override ModelState.IsValid property in my ModelView so that I could pass the type to it. But as far as i know there is no way to.

Are there any other solutions? (it preferrably should be able to use with client-side validation)

2
  • IMO the problem here is that you have a "ViewModel" that does not fit with your real view model. You should split it. Commented Dec 3, 2010 at 9:07
  • I know it's a wrong way and the main source of problems here, but this is a requirement. Commented Dec 3, 2010 at 9:12

1 Answer 1

2

If it is really a requirement to have only one ViewModel for two different views, "Partial Validation" could be your solution. Check out this blog:

http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.