별도의 div에 데이터를 표시하고 JavaScript 함수 데이터를 전달하면 onclick이 실행되지 않습니다. 그것은 부모로부터 데이터를 사용할 때 작동하지만, 부모로부터 자식으로 데이터를 캐스팅하고 그 데이터로 JS 함수를 전달하려고 시도한 후에는 작동하지 않습니다.자식 모델 데이터를 사용할 때 JavaScript 함수가 실행되지 않습니다.
##view##
@using WebApplication2.Models
@model IEnumerable<WebApplication2.Models.OBJECT>
@{
ViewBag.Title = "CompAndMon";
}
<script>
//this function does not fire when called
function setO(OfficeLocation,Name,Email,Phone,NumComputers,NumMonitors) {
var text = "Primary Contact Name " + Name+"\n Primary Contact Email: " +Email +"\n Primary Contact Phone: " +Phone +"\n Number of Computers: " +NumComputers +"\n Number Of Monitors: " +NumMonitors;
var location = "Office Location: " + OfficeLocation;
document.getElementById("Nametag").innerHTML = location;
return document.getElementById("OCM").innerHTML = text;
}
//this function does not fire when called
function setComputer(lastS) {
var text = "you selected Item No: " + lastS;
return document.getElementById("OCM").innerHTML = text;
}
//this function operates correctly
function setMonitor(id) {
var text = "you selected Item No: " + id;
return document.getElementById("OCM").innerHTML = text;
}
</script>
@foreach (var item in Model)
{
if (@item.Type == 1)
{
var office = item as Office;
var loc = @office.OfficeLocation;
var Name = @office.Name;
var email = @office.Email;
var phone = @office.Phone;
var mons = @office.NumMonitors;
var comps = @office.NumComputers;
<p><a onlick="setO(@loc,@Name,@email,@phone,@comps,@mons)">@office.Name</a></p>
}
else if (@item.Type == 2)
{
var computer = item as Computer;
<p>  <a onclick="setComputer(@computer.LastUser)">@item.Name1</a></p>
}
else
{
var monitor = item as Monitor;
<p>    <a onclick="setMonitor(@item.ID)">@item.Name1</a></p>
}
}
<h2 id="Nametag" style="text-align:center"></h2>
<div id ="OCM"class="row">
Select a computer or monitor and the information about the de will be displayed here.
</div>
여기 뷰 시스템을 사용하여 ## 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; }
}
}
내가 요소를 검사
는 데이터가 기능을 전달되고 있음을 보여줍니다 크롬,하지만 내가 할 무엇인지 확실하지 실행되지 않습니다. 어떤 도움이 동일합니다
Here shows that the data was passed to the JS functions when I inspect the element