【发布时间】:2019-12-21 22:11:25
【问题描述】:
我正在努力将 SignalR 添加到工作中的 MVC 应用程序,但我不熟练使用 Javascript,而且我不知道如何编写它来显示我需要的内容。我看到一堆关于聊天应用程序(发送消息)的教程,但这不是我需要的。
我想要的: 显示包含所有组件的完整模型。 程序接收一个在数据库中添加新属性的文件。该属性必须与其所有组件一起显示。
这是要显示的模型(属性)示例
public int WaterLevel { get; set; }
public int WindSpeed { get; set; }
public int NumberOfMachine { get; set; }
public int FoodLevel { get; set; }
html 页面是使用 Visual Studio 创建的默认 html 页面: (这是需要用 javascript 在网页上更新的内容)
<tbody>
@foreach (var item in Model)
{
<tr id="properties_display">
<td id="waterLevel">
@Html.DisplayFor(modelItem => item.WaterLevel)
</td>
<td id="windSpeed">
@Html.DisplayFor(modelItem => item.WindSpeed)
</td>
<td id="numberMachine">
@Html.DisplayFor(modelItem => item.NumberOfMachine)
</td>
<td id="foodLevel">
@Html.DisplayFor(modelItem => item.FoodLevel)
</td>
<td id="number">
@Html.DisplayFor(modelItem => item.Number)
</td>
</tr>
}
</tbody>
观察者(On_Change):
private readonly IHubContext<PropertyHub> _hubContext;
public MyFileWatcher(IHubContext<PropertyHub> hubContext)
{
_hubContext = hubContext;
//watcher stuff
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
DataAccess db = new DataAccess(MVCCoreAppDB);
string filePath = Path.GetFullPath(e.FullPath);
db.InsertData(filePath);
var propData = db.LoadData();
List<PropertyModel> properties = new List<PropertyModel>();
foreach (var row in propData)
{
properties.Add(new PropertyModel
{
WaterLevel = row.WaterLevel,
WindSpeed = row.WindSpeed,
NumberOfMachine = row.NumberOfMachine,
FoodLevel = row.FoodLevel
Number = row.Number
});
}
_hubContext.Clients.All.SendAsync("onFileChange", properties);
}
观察者类正在寻找文件夹中的变化。创建或修改文件时,它会调用 3 个方法。首先,它将数据插入数据库,然后从数据库中加载数据并更改要使用的数据类型。
最后,它调用集线器让它知道有变化并且它需要更新网页。
这是我尝试过的,我得到了我需要的实时效果,但它没有显示正确的东西:
"use strict";
let connection = new signalR.HubConnectionBuilder()
.withUrl("/propertyHub")
.build();
connection.on("onFileChange", function (properties) {
let tr = document.createElement("tr");
document.getElementById("properties_display").appendChild(tr);
let tdWaterLevel = document.createElement("td");
tdWaterLevel.textContent = properties[0];
document.getElementById("waterLevel").appendChild(tdWaterLevel);
let tdWindSpeed = document.createElement("td");
tdWindSpeed.textContent = properties[1];
document.getElementById("windSpeed").appendChild(tdWindSpeed);
let tdNumberMachine = document.createElement("td");
tdNumberMachine.textContent = properties[2];
document.getElementById("numberMachine").appendChild(tdNumberMachine);
let tdFoodLevel = document.createElement("td");
tdFoodLevel.textContent = properties[3];
document.getElementById("foodLevel").appendChild(tdFoodLevel);
let tdNumber = document.createElement("td");
tdNumber.textContent = properties[4];
document.getElementById("number").appendChild(tdNumber);
});
connection.start().then(function () { TestConnection(); }).catch(function(err)
{
return console.error(err.toString());
});
function TestConnection() {
connection.invoke("GetConnectionId").catch(function (err) {
return console.error(err.toString());
});
}
就像现在一样,它写在好的列中,但不是在表的末尾,也不是写值。它正在写对象而不是数字。 此外,当我在 javascript 中编写时;
properties.WaterLevel //For example
它是放置一个空格而不是写一些东西。
//It does the same thing if I write properties [0].WaterLevel
更新前的输出: 更新后的输出: 所需的输出: 添加新文件后调试中的属性值:
知道如何在一个好地方获得正确的值吗?
【问题讨论】:
-
能否将“console.log(JSON.stringify(properties))”的输出粘贴到“onFileChange”函数中
-
向我们展示一些输出,请它在浏览器中显示什么?
标签: javascript c# asp.net-mvc asp.net-core signalr