when we need to pass multiple data items using a view model from a controller to a view, for example, to pass both a collection of products{id, brand, price, name} and collection of product categories{id,type} to a view, how can i validate both of these
two entities???
if there is only one entity "products in the model", the validation should be somewhat like this
public partial class products
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(brand))
yield return new RuleViolation("brand required", "brand");
if (String.IsNullOrEmpty(price.ToString()))
yield return new RuleViolation("price required", "price");
if (String.IsNullOrEmpty(name))
yield return new RuleViolation("name required", "name");
yield break;
}
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
1. Validate each (products, product categories) and do ModelState.AddModelError("name of the property", "errormessage")
or
2.Since ASP.NET MVC is aware of IDataErrorInfo then implement IDataErrorInfo on ViewModel and return rule violation from (products, product categories)
mvc_noob
Member
4 Points
34 Posts
when using ViewModel pass mutiple entites, how to validate them?
Jun 11, 2010 04:46 AM|LINK
when we need to pass multiple data items using a view model from a controller to a view, for example, to pass both a collection of products{id, brand, price, name} and collection of product categories{id,type} to a view, how can i validate both of these two entities???
if there is only one entity "products in the model", the validation should be somewhat like this
public partial class products { public bool IsValid { get { return (GetRuleViolations().Count() == 0); } } public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(brand)) yield return new RuleViolation("brand required", "brand"); if (String.IsNullOrEmpty(price.ToString())) yield return new RuleViolation("price required", "price"); if (String.IsNullOrEmpty(name)) yield return new RuleViolation("name required", "name"); yield break; } public class RuleViolation { public string ErrorMessage { get; private set; } public string PropertyName { get; private set; } public RuleViolation(string errorMessage, string propertyName) { ErrorMessage = errorMessage; PropertyName = propertyName; } }how about for two entities now??????
ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: when using ViewModel pass mutiple entites, how to validate them?
Jun 11, 2010 07:12 AM|LINK
1. Validate each (products, product categories) and do ModelState.AddModelError("name of the property", "errormessage")
or
2.Since ASP.NET MVC is aware of IDataErrorInfo then implement IDataErrorInfo on ViewModel and return rule violation from (products, product categories)
(or better, implement IDataErrorInfo on both )
mvc_noob
Member
4 Points
34 Posts
Re: when using ViewModel pass mutiple entites, how to validate them?
Jun 15, 2010 12:40 AM|LINK
thanks so much