1

I am new to MVC but have worked my way through the validation tutorials and they do exactly what I want to do... but.... my model is in a separate portable class library.

How would I add the validation rules to this non-MVC solution so that my MVC website?

Is it possible please?

Thanks

Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89

1 Answers1

3

You can create an interface to that class and use impromptu interface to have your class act as that interface...

Lets say this is the class from the portable library:

public class SomeClass
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Create a cloned interface and specify validation attributes in it:

public interface ISomeClass
{
    [Required]
    string FirstName { get; set; }
    string LastName { get; set; }
} 

At the top of your view, pass the interface instead of the class:

@model YourNamespace.Models.ISomeClass

In your controller, do:

return View(instanceOfSomeClass.ActLike<ISomeClass>();

You can find impromptu interface here: http://code.google.com/p/impromptu-interface/

Since the class and the interface look exactly the same, model binding works as well.

Hope this helps.

Antevirus
  • 156
  • 6