0

I have applied some validation with data annotations but somehow I am missing something in the code.

public class Person 
{
    public people SinglePerson { get; set; }
    public IEnumerable<SelectListItem> ColorNames { get; set; }
    public IEnumerable<SelectListItem> WebCustomer { get; set; }
    public IEnumerable<SelectListItem> PreviouslyOredered { get; set; }
}

and here is my cs class

[MetadataType(typeof(peopleMetaData))]
public partial class people
{
}

public class peopleMetaData
{
    [Required(ErrorMessage = "Please enter a name")]
    [StringLength(50, MinimumLength = 2)]
    public string firstName { get; set; }
}

The People class has a firstName property that I want to do some validation to.

What am I missing?

2
  • did you apply the check for model in the controller ?? Commented Aug 23, 2017 at 22:56
  • Hi, what check? Commented Aug 23, 2017 at 23:05

5 Answers 5

2

In the metadata class, you must not specify your "properties" as full properties - just the type and name is needed - try this:

public class peopleMetaData
{
    [Required(ErrorMessage = "Please enter a name")]
    [StringLength(50, MinimumLength = 2)]
    public string firstName;
}

See - no { get; set; } for the firstName "property" here

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

3 Comments

My solution now woks with {get;set;} specified. Is it better to leave it from the solution for some kind of code readability?
@laz: so what did you do / change to make it work now??
i added the "jqueryval" to the layout page (as suggested by @OrElse) and i used the "Model.IsValid" in the controller (as suggested by @moath naj)
1

For client side validation, make sure you have included @section Scripts { @Scripts.Render("~/bundles/jqueryval") } within your view

2 Comments

That was the problem. Thanks for the response.
@laz no worries :)
1

Partial classes can only exist within one project.

http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx

More specifically,

All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.

Comments

0

you have to check if the model is valid or not in the controller

if (ModelState.IsValid) {
  //do something 
}
else
{
return View();
}

2 Comments

Thanks for the catch. It work's now but I don't get the validation message to be displayed.
you are welcome if you can mark answer as correct to avoid other answers
0

You people Class doesn't have a property of firstName. You need that in your people class first, then the MetadataTypeClass

[MetadataType(typeof(peopleMetaData))]
public partial class people
{
    public string firstName { get; set; }
}

1 Comment

the "people " is auto generated. I have a SQL table with that name that I have imported in with LINQ to SQL layer.

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.