【发布时间】:2016-09-11 06:29:32
【问题描述】:
我正在使用 MVC Razor 尝试让一个链接列表出现在屏幕的左侧下方并且可以点击,然后将从该模型中检索数据并显示在屏幕的单独部分中。在这个视图中,我将 Office、Computers 和 Monitors 的模型存储在一个名为 OCM.cs 的超级模型类中,如下所示:
##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; }
}
}
在此之后,我创建了一个硬编码的 OBJECT 列表,因为我还没有附加数据库。
##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);
}
在此之后,我使用 foreach 循环在屏幕左侧显示 OBJECTS 列表。我用“a”标签中的对象名称显示它们。我将操作 onclick 放在“a”标签内,希望将模型数据传递给 java 脚本函数,然后该函数将在屏幕的另一部分显示该信息。有没有办法从提供给视图的 OBJECT 模型列表中获取存储在办公室计算机或监视器中的信息(序列号、制造商、FreeHDSpace 等)?
##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>  
<a onclick="getItem(@item)">
@item.Name1
</a>
</p>
}
else
{
<p>    
<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>
【问题讨论】:
-
您必须先转换为子类型,然后才能访问其属性。例如
if(item is Office) { var location = ((Office)item).OfficeLocation; } -
不能这样做,因为 Office 类不包含在 OBJECT 命名空间中
-
添加
@using WebApplication2.Models以便您可以访问视图中的所有短模型名称。否则,您需要为类指定完整的命名空间。 -
有什么理由不能将我如您上面所说的创建的这些变量传递给 javascript 函数吗?当我检查元素时,它显示对象正在为不同的对象正确传递,但是 onclick 没有使用这些变量运行函数@Jasen
标签: javascript c# html asp.net-mvc-4 razor