I'm very new to ASP.NET MVC (as well as ASP.NET in general), and going to gain some knowledge for this technology, so I'm sorry I can ask some trivial questions. I have installed ASP.NET MVC 3 RC1 and I'm trying to do the following.
Let's consider that I have a model that's completely auto-generated from a table using the "LINQ to SQL Classes" template in VS2010. The template generates 3 files (two .cs files and one .layout file respectively), and the generated partial class is expected to be used as an MVC model. Let's also consider, a single DB column, that's mapped into the model, may look like this:
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Name", DbType = "VarChar(128)")]
public string Name {
get {
return this._Name;
}
set {
if ( (this._Name != value) ) {
// ... generated stuff goes here
}
}
}
The ASP.NET MVC engine also provides a beautiful declarative way to specify some additional stuff, like RequiredAttribute
, DisplayNameAttribute
and other nice attributes. But since the mapped model is a purely auto-genereated model, I've realized that I should not change the model manually, and specify the fields like:
[Required]
[DisplayName("Project name")]
[StringLength(128)]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Name", DbType = "VarChar(128)")]
public string Name {
...
though this approach works perfectly... until I change the model in the DBML-designer removing the ASP.NET MVC attributes automatically. So, how do I specify ASP.NET MVC attributes for the DBML models and their fields safely?
Thanks in advance, and Merry Chris开发者_Python百科tmas.
Update #1:
While expecting for responses, I have found such stuff:
- http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx - does not work for me
- MetadataType and client validation in ASP.NET MVC 2 - the mine seems to be a duplicate of the one
- http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/ - promises the most for me
The more correct approach is to make corresponding DTO's (ViewModels or whatsoever you call it) and Map those DTO's to your Data Model Objects with tools like AutoMapper(translate your fat data model objects to clean objects and vice versa).
There exists some tools that will do this task(Making DTO's) for you and generate those DTOs.
Look at VS gallery and chances are you find many options.
Maybe finding a nice sample like MicrosoftNLayerApp employing all these techniques will guide you throughout this process.
And finally your answer is to annotate DTO class properties with DisplayName attribute.
Since I do not want to create any helper classes, I found the article that meets my requirements the most. However, that approach does not work for some reason, but I still believe this is because of my lack of ASP.NET MVC experience (see Step 4: Creating a Custom [Email] Validation Attribute
):
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
精彩评论