0

I wonder if there is a way to validate just one of my models in the viewmodel send it to my action? I use the DataAnnotations as validate rules.

Like the if (!ModelState.IsValid)

let me know if the question is unclear and I will edit for a better explination

EDIT my viewmodel looks like this

public class CompaniesViewModel
{
    public Core.Model.Customer Company { get; set; }        
    public IEnumerable<SelectListItem> Items { get; set; }        
    public Core.Model.Meeting Meeting { get; set; }
}

What I want to do in this particular situation is to validate just Customer. I cant do the ModelState.IsValid then all get validated. So how can I do to just validate one of them like customer in this case. Hope this was more clear

Dejan.S
  • 18,571
  • 22
  • 69
  • 112

3 Answers3

1

There are a number of different ways you can do this. The first is to add a property called IsValid that checks the property. So something like:

public class Company
{
  public bool IsValid
  {
    get { return GetValid() }
  }

  private bool IsValid()
  {
    if ( Some check here )
      return false;
  }
}


[HttpPost]
public ActionResult SomeAction(CompaniesViewModel model)
{
  if (model.Company.IsValid)
  {
  }
}

However a better solution IMO would be just to post the Company to your controller rather than your entire view model. Just because your passing a view model to a view it doesn't mean that you need to post the entire view model back. When you create your HTML form specify only the properties you want to post back to your controller. So for example your controller would become:

[HttpPost]
public ActionResult SomeAction(Company company)
{
  if (Model.IsValid)
  {
  }
}

Now when you check if Model.IsValid it just check company as that is all you've passed back to the controller.

lancscoder
  • 8,658
  • 8
  • 48
  • 68
0

You can separate Customer Model to another class in your ViewModel and map that in Controller to a existing/new Customer:

public class CompaniesViewModel
{
    public Company Company { get; set; }        
    public IEnumerable<SelectListItem> Items { get; set; }        
    public Core.Model.Meeting Meeting { get; set; }
}

//Validations for Company go here:
public class Company
{
    public string CompanyId { get; set; }

    [Required]
    public string CompanyName { get; set; }
}
Acaz Souza
  • 8,311
  • 11
  • 54
  • 97
0

At server side, you can try the ValidateModel(object) method, like in TryValidateModel(CompaniesViewModel.Company).

If you have enabled client sided validation, you need to post only the relevant entity. If you want to post all entities, but you need to validate only one, you can consider the following:

  1. either removing the rules, using javascript ASP .NET MVC Disable Client Side Validation at Per-Field Level
  2. or creating a Data-Transfer-Object, ie a View Model which has NO link to the Model, but reproduces the entities you want with the validation rules you want having applied in this scenario. Of course, then, you'll need in your controller or a model binder some way to bind from your ViewModel to your Model entities.
Community
  • 1
  • 1
tec-goblin
  • 196
  • 1
  • 11