Nick Zulanas Nick Zulanas - 2 months ago 14
C# Question

In MVC how can you retrieve data from a model that is stored as a object in its super model?

I am using MVC Razor to try to have a list of links appear down the left side of the screen and be clickable which will then retrieve data from that model and be displayed in a separate section of the screen. In this view I store models for Office, Computers and Monitors in a super model class called OCM.cs Shown below:

##OCM.cs##

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.Models
{
public abstract class OBJECT
{
public int ID { get; set; }
public int Type { get; set; }
public string Name1 { get; set; }
}
public class Office:OBJECT
{

public string OfficeLocation { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public int NumComputers { get; set; }
public int NumMonitors { get; set; }
}
public class Computer:OBJECT
{

public String LastUser { get; set; }
public String Name { get; set; }
public int NumMonitors { get; set; }
public String TotalHDSpace { get; set; }
public String FreeHDSpace { get; set; }
public int NumUpdates { get; set; }
}
public class Monitor:OBJECT
{

public String Manufacturer { get; set; }
public String ModelID { get; set; }
public String SerialNum { get; set; }
public int HoursON { get; set; }
public String LastTestTime { get; set; }
public String LastTestType { get; set; }
}
}


After this I created a hardcoded OBJECT list because I have not attached a database yet.

##Controller for view for CompAndMonitors##
public ActionResult CompAndMon()
{
var ObjectList = new List<OBJECT>{
new Office() {Type = 1,ID = 1, Name1 = "Fort Collins", Name = "Fort Collins", Email = "pasty@gmail.com", Phone = "555-123-5555", NumComputers = 1, NumMonitors = 1, OfficeLocation = "Fort Collins"} ,
new Computer() {Type = 2,ID = 2,Name1 = "Speed-Machine", Name = "Speed-Machine", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } ,
new Monitor() {Type = 3, ID = 3, Name1 = "Sony", Manufacturer = "Sony", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a36-f45-gh325"} ,
new Office() {Type = 1, ID = 4, Name1 = "Denver", Name = "Denver", Email = "pasty@gmail.com", Phone = "555-123-5555", NumComputers = 2, NumMonitors = 3, OfficeLocation = "Denver"} ,
new Computer() {Type = 2, ID = 5, Name1 = "Nicks PC", Name = "Nick's PC", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } ,
new Monitor() {Type = 3, ID = 6, Name1 = "LG", Manufacturer = "LG", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a38-l87kp-g6j9"} ,
new Computer() {Type = 2, ID = 7, Name1 = "Ted", Name = "FastOne", LastUser = "Ted", NumMonitors = 2, FreeHDSpace = "23GB", NumUpdates = 2, TotalHDSpace = "50GB" } ,
new Monitor() {Type = 3, ID = 8, Name1 = "HTC", Manufacturer = "HTC", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "d77-ko9-poo77" },
new Monitor() {Type = 3, ID = 9, Name1 = "Panisonic", Manufacturer = "Panisonic",HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "h67-j567-lo99" }
};

return View(ObjectList);
}


After this I use a foreach loop to display the list of OBJECTS on the left side of the screen. I display them by the name of the object in an 'a' tag. I put the action onclick inside of the 'a' tag to hopefully pass the model data to a java script function which will then display that information on another part of the screen. Is there a way to get the information(SerialNumber, Manufacturer, FreeHDSpace, etc..) stored in the Office Computer or Monitor from the list of OBJECT model given to the view?

##View CompAndMon##
@model IEnumerable<WebApplication2.Models.OBJECT>
@{
ViewBag.Title = "CompAndMon";
}
<script>
//I would like the function to look something like this
function getItem(Model){
//though I know this next line is not possible
var text = "this is the Serial Number" + @Model.SerialNum;
document.getElementBtId("displaytab").innerHTML = text;
}
</script>
@foreach (var item in Model)
{
if (@item.Type == 1)
{
<p>
<a onclick="getItem(@item ) ">
@item.Name1
</a>
</p>
}
else if (@item.Type == 2)
{
<p>&ensp;&ensp;
<a onclick="getItem(@item)">
@item.Name1
</a>
</p>
}
else
{
<p>&ensp;&ensp;&ensp;&ensp;
<a onclick="getItem(@item)">
@item.Name1
</a>
</p>
}
}
<div id="displaytab" class="container col-lg-11 col-lg-offset-1">
Select a computer or monitor and the information about the device or loaction will be displayed here.
</div>

Answer

You can use this (for example for "computers") by casting them to their specific classes:

var office = item as Office;
var computer = item as Computer;
var monitor = item as Monitor;
if (office != null)
{
     <p>
          <a onclick="getItem(@item ) ">
               @item.Name1, @office.OfficeLocation;    <== office "knows" properties
           </a>
     </p>
}

Sidenote: Perhaps the Type property is not needed anymore.

Comments