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

1 Answer 1

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.

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

Comments

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.