2

I have a View like this (unimportant stuff left out):

@model MyProject.Models.Accounts

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
        </div>

Now, I have a separate Model I want to use for validation. Is it possible to use both my Model for data AND this validation Model in a View? Something like this:

@model MyProject.Models.Accounts
@validaitonmodel MyProject.Models.AccountValidationModel

            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(validationmodel => validationmodel.Name)
            </div>

Thanks in advance.

EDIT

By "Model" I mean the models that are automaticly generated by the .edmx (the Ado.net self-tracking entities)

By ValidationModel I mean a custom class like this:

public class AccountValidationModel
{
    [Required]
    public String Name {get; set;}
}
1
  • I'm a little confused by your question. Can you clarify a bit by what you mean "you have a separate model you want to use for validation." Can you give a more concrete example? Commented May 13, 2011 at 15:01

2 Answers 2

3

I think the generally more acceptable practice is to either:

  1. Add the attributes to your data model.
  2. Bind your view to a separate ViewModel (in this case, your AccountValidationModel), and use a mapper tool (like AutoMapper) to map the data between your two models.
Sign up to request clarification or add additional context in comments.

2 Comments

i like #2. keeps your data and business layers out of the view.
Thanks, AutoMapper looks like the thing I need.
1

May be the MetadataType attribute could help you? http://msdn.microsoft.com/en-us/library/ee256141.aspx

The Validation Model must implement all properties to validated, and MetadataTypeAttribute must be apply on the Model itself.

Quick sample from stackoverflow: ASP.Net C# validating model based on MetadataType

Or, like the guy, take a look for another way to validation with fluentvalidation (never tried) http://fluentvalidation.codeplex.com/wikipage?title=mvc&referringTitle=Documentation

2 Comments

But what if I have fields in my view like "ConfirmName" that need to be validated but are not present in my Model?
Yeah, this is why Domain Model is different of View Model. Like @Jerad Rose said: create a ViewModel and map properties with AutoMapper is probably the best and the simplest way. codeclimber.net.nz/archive/2009/10/27/… Also, you can easly set the ModelBinder Attribute on a ViewModel.

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.