views:

54

answers:

4

I'm really not sure how to ask this, so forgive me if it sounds a bit off.

I have an IPerson interface, a Student : IPerson class and an Employee : IPerson in the Project.Data namespace.

In my controller, I add the reference for Project.Data and Project.Services and add using statements where appropriate.

In my view, I create a strongly-typed view to Project.Data.IPerson - so I can dynamically render views based on type.

What I want is for Model.getType() to return "Student" not "Project.Data.Student" - is this possible?

+1  A: 

Model.GetType().Name will return Student. In your view you could test the type like this:

<% if (Model is Student) { %>
    <div>Student</div>
<% } else if (Model is Employee) { %>
    <div>Employee</div>
<% } %>

UPDATE:

Add the following to the beginning of the view:

<%@ Import Namespace="Project.Data" %>
Darin Dimitrov
Why not: <div><%: Model.GetType().Name %></div>?
jfar
The problem I have is I can't say "Student" - I have to say "Project.Data.Student". It seems the view can't resolve the objects (even IPerson) without the full namespace
Dan
Import directives in View?
mare
A: 

Yeah Darin is right but I'm not sure that using interface will actually work, because you have an interface for your strongly typed view. I would consider using abstract class instead. Coz its really semantically wrong to use interfaces like you do.

search for "interface vs. abstract class" here on Stack, you should find many questions dealing with it..

mare
Good stuff, Marko - I'm just looking into this problem so I'm eager to read all I can on the best approach!
Dan
+1  A: 

Add a new read only member

class Project.Data.IPerson{
     public string Role{get; }
}
class Project.Data.Student : IPerson{
     public string Role{get{return "Student"; }
}
class Project.Data.Employee : IPerson{
     public string Role{get{return "Employee"; }
}
Dennis Cheung
I actually have a "PersonType" enum I reference via LINQ
Dan
A: 

I had to add the namespaces in the web.config facepalm

Dan