Pregunta

I have a view model that a group of partial pages in tabs. When a tab is selected, it post the current tab and calls the partial for the next tab. A single view model is used for this form, with each tab broken out to it's own submodel. I can check which submodel has been submitted, but how do I check the validation on each tab independently.

public class formView
{
  public formHeader fHeader {get;set;}
  public formTab1 fTab1 {get;set;}
  public formTab2 fTab2 {get;set;}
  ...
}

public class formHeader
{
  [Required]
  public string name {get;set;}
  [Required]
  public DateTime dtDob {get;set;}
  ...
}

public class formTab1
{
  [Required]
  public string address1 {get;set;}
  public string address2 {get;set;}
  [Required]
  public string city {get;set;}
  ...
}

public class formTab2
{
  [Required]
  public string email {get;set;}
  public string cellPhone {get;set;}
  ...
}
...

When Posted, Header and 1 tab are submitted on the formView model. I have no problem accessing this and saving it, but how do I validate fView.fTab# as a section and not the whole of formView? Between a couple different forms I have around 17 tabs and a couple hundred data points that are being stored.

¿Fue útil?

Solución

Call TryUpdateModel(model.yourCurrentSubModel) before checking ModelState.IsValid().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top