Custom validation in ASP.NET MVC - asp.net-mvc-3

I have a Page object that contains a Metadata property like this
public class Page {
public int Id { get; set; }
public int ParentId { get; set; }
public Metadata Metadata { get; set; }
}
public class Metadata {
public string Slug { get; set; }
}
when I save my page I need to verify that no other page with the same parent has the same slug. I was thinking about using a validation attribute on the slug property but when I do that I'm not able to find the page object. What is the best approach of validating such things?

If you insist upon using data annotations validation attributes, you could get access to all of the properties by putting the attribute on the Page class rather than the Slug property.
However there is something better.

Related

Is there a way to conditionally validate a model based on the http verb used?

I'm writing an API and have a very simple model
public class CategoryModel
{
public int ID { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
public string Description { get; set; }
}
I don't want to enforce that the ID property is required if the model is coming in via a post action, but I do if it's a put action. Are there any validation attributes that allow for this or do I need to create a separate model for post and put?
I'm just learning this so I could be doing it wrong altogether so a point in the right direction would be appreciated!
Thanks!
We can do by use fluentvalidation library or create customer attribute

Navigating ID properties to source with an OData API

I have a class which is exposed by an ASP.NET OData V4 API:
public class Person
{
public int TitleId { get; set; }
//Other properties like First Name, Last Name, DoB and so on...
}
The TitleId property is an Id that maps to a persons Title (Mr, Miss, Mrs so on) which is located at a different OData endpoint address.
Is there any way of providing metadata to let the consumer of the API know where the lookup values are for this property or should I approach this in a different way?
Edit: I am looking for a way to provide metadata to inform the consumer of the location of the lookup values. There is no database involved, this is a mapping layer over a much more complicated API.
if you have the Class of Title and have that in the Database this is working:
public class Title
{
public int Id { get; set; }
public string TitleName { get; set; }
}
then use this that as Virtual in your current model.
[ForeignKey("TitleId")]
public virtual Title { get; set; }
public int TitleId{ get; set; }
I'm sure it does the mapping.
then just address them to Title.TitleName.

Is there a way remove property using Data Annotation in Asp.net MVC3?

Public class UserMetdata
{
[Required]
[Display(Name="User ID")]
public int UserID { get; set; }
[Display(Name="User Name")]
public string UserName { get; set; }
}
I dont want to UserName to be shown in View. Its similar like creating not required Annotation. One solution is by deleting UserName form Class but i dont want that.
How can it be done using Data Annotation.
You could use ScaffoldColumnAttribute for that property
[ScaffoldColumn(false)]
public string UserName { get; set; }
This will work only when you let framework dynamically generate your views by calling #Html.DisplayForModel() or like, and you DO NOT have defined display template for that model at Views/Shared/DisplayTemplates or Views/ControllerName/DisplayTemplates. Otherwise, you should edit that display template and remove corresponding line from it

MVC3: Attribute for not mapping a property to a DB column

I’m using ASP.NET MVC3. I have a model that has one property that I don’t want to store in the database. Is there an attribute that I can put on the property to achieve this? Thanks.
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public string FullName { get; set; }
}
The attribute are in the namespace System.ComponentModel.DataAnnotations
Just to add more options... this is why I prefer to keep my domain model separate from my view model. My view model often has additional fields necessary for rendering the view which does not belong in the domain model. The design I typically use is described pretty well here.

Required attribute for complex objects

I have a class like this
public class PageReference {
[ScaffoldColumn(false)]
public string Id { get; set; }
public string Name { get; set; }
}
and in my model I use it like this
[Required]
public PageReference PageLink { get; set; }
the required attribute does not fire if I add it to the pagelink property, how can this be solved?
The validation attribute is evaluated by the model binder against the data supplied by the value provider (often posted form fields). If you're posting a form that does not include that field, the binder won't touch that property of the model and so won't evaluate the validation attributes.
I think there is no recursive validation support in asp.net mvc

Resources