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 8360107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:21:41+00:00 2026-06-09T11:21:41+00:00

How to cast Model (which is interface type) to its actual type, please? What

  • 0

How to cast Model (which is interface type) to its actual type, please?

What you see below is obviously much simplified version concentrating on problem at hand. So question is not how to re-architect my solution, but specifically how to cast interface type model instance to actual class type in razor.

View Model:

public interface IData
{
  string Data { get; set; }
}
public class Child1 : IData
{
  [Display(Name = "Name1", Description = "Desc1", Prompt = "Prompt1")]
  public string Data { get; set; }
}
public class Child2 : IData
{
  [Display(Name = "Name2", Description = "Desc2", Prompt = "Prompt2")]
  public string Data { get; set; }
}
// It gets to Child10 and there is dynamic number of each of them in the main ViewModel

In the view I need to display data annotations, but MVC won’t do it in object oriented fasion.
If I just say

@model <path to>.IData
@Html.LabelFor(m => model.Data)
@Html.TextBoxFor(m => model.Data)
@Html.ValidationMessageFor(m => model.Data)

I get what’s defined on interface, rather than what is on the actual class.
Stupid solution is to make huge switch and cast Model to the actual type it comes from:

@switch(Model.GetType().Name)
{
  case "Child1":
    var c1 = Model as Child1;
    @Html.LabelFor(m => c1.Data)
    @Html.TextBoxFor(m => c1.Data)
    @Html.ValidationMessageFor(m => c1.Data)
    break;
  // etc 
}

Switch becomes unmanageable in case I have more than 3 children of IBase (and I do), let alone looks ugly.
Instead of doing the switch, how can I use reflection to get Model’s type?
Note that simple

@{
  var c1 = Model as Model.GetType();
  var c2 = (Model.GetType()) Model;
  var c3 = (Type.GetType(Model.GetType())) Model;
  var c4 = Model as Type.GetType(Model.GetType();
}

etc gives scary warnings and certainly doesn’t run in razor.

I am sure it is possible to get by reflection from a model, but no matter what I’ve tried doesn’t work.
Hope you guys have some fresh ideas.

Small Update

I cannot combine Child where X is number into one model because Its not known in advance how many of them are there, in the main view its possible to add/delete those. The picture I drew is very specific to this problem, views/controllers/models are significantly more complicated than you see here.

Thanks in advance.

  • 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-06-09T11:21:43+00:00Added an answer on June 9, 2026 at 11:21 am

    A quick note about your example: you show the interface of IBase, but your classes are implementing the interface IData.

    Instead of having your model be the interface, your model should be an object of the actual type you’re going to work with (or have properties that contain the types you want to work with).

    As an example, if you want to display data about both childs in your view, setup your model like this:

    public class ChildModel {
      public Child1 { get; set; }
      public Child2 { get; set; }
    }
    public class Child1 : IData
    {
      [Display(Name = "Name1", Description = "Desc1", Prompt = "Prompt1")]
      public string Data { get; set; }
    }
    public class Child2 : IData
    {
      [Display(Name = "Name2", Description = "Desc2", Prompt = "Prompt2")]
      public string Data { get; set; }
    }
    

    Where you return your ChildModel to your view and then you don’t need to do any casting within your view (which you should avoid anyway).

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

Sidebar

Related Questions

How to cast an object (of type Object) into its real type? I need
LinQ contains the method Cast which casts each entry in the list to type
Unable to cast object type System.Collections.Generic.List 1[NorthwindMVC3.Models.Product] to the type ofSystem.Collections.Generic.IEnumerator 1[NorthwindMVC3.Models.Product].\ public IEnumerator<Product>
I want to write a common method which returns any model like product, sales,etc.
I'll just jump right into it. I have a model called Request which I
I get this error when I post back the fields below: Unable to cast
I have a client application (WPF, C#, .net4) which uses POCO entity model connected
I am trying to design a model in which I have a List of
I have a view which accepts the following model: Inherits=System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel> This posts back to
I want to implement an Observer of a Model class which does not change

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.