Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Model casting

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.

like image 546
Display Name Avatar asked Jan 27 '26 19:01

Display Name


1 Answers

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).

like image 103
Justin Helgerson Avatar answered Jan 30 '26 15:01

Justin Helgerson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!