Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:42:16+00:00 2026-05-16T10:42:16+00:00

In my ASP.NET MVC app, I have an interface which acts as the template

  • 0

In my ASP.NET MVC app, I have an interface which acts as the template for several different view models:

public interface IMyViewModel
{
    Client Client1 { get; set; }
    Client Client2 { get; set; }

    Validator Validate();
}

So, my view models are defined like this:

public interface MyViewModel1 : IMyViewModel
{
    Client Client1 { get; set; }
    Client Client2 { get; set; }

    // Properties specific to MyViewModel1 here

    public Validator Validate()
    {
        // Do ViewModel-specific validation here
    }
}

public interface MyViewModel2 : IMyViewModel
{
    Client Client1 { get; set; }
    Client Client2 { get; set; }

    // Properties specific to MyViewModel2 here

    public Validator Validate()
    {
        // Do ViewModel-specific validation here
    }
}

Then I currently have a separate controller action to do the validation for each different type, using model binding:

[HttpPost]
public ActionResult MyViewModel1Validator(MyViewModel1 model)
{
    var validator = model.Validate();

    var output = from Error e in validator.Errors
                 select new { Field = e.FieldName, Message = e.Message };

    return Json(output);
}

[HttpPost]
public ActionResult MyViewModel2Validator(MyViewModel2 model)
{
    var validator = model.Validate();

    var output = from Error e in validator.Errors
                 select new { Field = e.FieldName, Message = e.Message };

    return Json(output);
}

This works fine—but if I had 30 different view model types then there would have to be 30 separate controller actions, all with identical code apart from the method signature, which seems like bad practice.

My question is, how can I consolidate these validation actions so that I can pass any kind of view model in and call it’s Validate() method, without caring about which type it is?

At first I tried using the interface itself as the action parameter:

public ActionResult MyViewModelValidator(IMyViewModel model)...

But this didn’t work: I get a Cannot create an instance of an interface exception. I thought an instance of the model would be passed into the controller action, but apparently this is not the case.

I’m sure I’m missing something simple. Or perhaps I’ve just approached this all wrong. Can anyone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:42:16+00:00Added an answer on May 16, 2026 at 10:42 am

    The reason why you cannot use the interface is because of serialization. When a request comes in it only contains string key/value pairs that represent the object:

    "Client1.Name" = "John"
    "Client2.Name" = "Susan"
    

    When the action method gets invoked the MVC runtime tries to create values to populate the method’s parameters (via a process called model binding). It uses the type of the parameter to infer how to create it. As you’ve noticed, the parameter cannot be an interface or any other abstract type because the runtime cannot create an instance of it. It needs a concrete type.

    If you want to remove repeated code you could write a helper:

    [HttpPost]         
    public ActionResult MyViewModel1Validator(MyViewModel1 model)         
    {         
        return ValidateHelper(model);         
    }         
    
    [HttpPost]         
    public ActionResult MyViewModel2Validator(MyViewModel2 model)         
    {         
        return ValidateHelper(model);         
    }
    
    private ActionResult ValidateHelper(IMyViewModel model) {
        var validator = model.Validate();         
    
        var output = from Error e in validator.Errors         
                     select new { Field = e.FieldName, Message = e.Message };         
    
        return Json(output);
    }
    

    However, you will still need a different action method for each model type. Perhaps there are other ways you could refactor your code. It seems the only difference in your model classes is the validataion behavior. You could find a different way to encode the validation type in your model class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building my first ASP.Net MVC based app and have a problem accessing
I have an asp.net mvc app running on a local iis website that is
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
I have an interesting situation where I need to deploy an ASP.NET MVC app
Background I have a page on my ASP.NET MVC web app for users to
I have a mixed UI (Win App, WPF App, and soon an ASP.NET MVC
In my asp.net mvc app I want to check if a certain url returns
I am working on an ASP.NET MVC web app that allows people to publish
I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually
I'm creating a small app in ASP.NET MVC that generates ics (iCal) files based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.