![](/img/trans.png)
[英]ASP.NET MVC - How to display data in Razor view returned from Webapi controller
[英]How to display the data to view from controller in asp.net MVC (Serial Communication)
我正在嘗試使用asp.net MVC c#中的Arduino和RFID閱讀器構建登錄系統。
我的目標是每次客戶將RFID標簽貼在RFID閱讀器上時,都會從控制器更新我的視圖。 每當客戶將RFID標簽點擊到RFID閱讀器時,我想更新視圖以顯示客戶端的信息。
現在,我可以使用下面的代碼從Arduino讀取標簽ID,但我不知道如何顯示我要查看的數據。 我確實在谷歌搜索這個問題,但我沒有得到asp.net的答案。
調節器
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
namespace read_rfid_example.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if(!mySerialPort.IsOpen)
mySerialPort.Open();
return View();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
Debug.WriteLine(indata); // TAG ID: 03 0e 03 06 (output example and want to pass this data to view)
}
}
}
簡單的看法
@{
ViewBag.Title = "Index";
}
<h3>@ViewBag.TagId</h3>
我的問題和我的問題是,每當RFID閱讀器檢測到RFID標簽時,我如何在視圖中更新我的標簽? 我需要幫助解決這個問題。 有人能給我一個正確的方法讓它像實時一樣嗎?
如果你想看到一些可能會像
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
Session["RfidCurrent"] = indata;
Debug.WriteLine(indata); // TAG ID: 03 0e 03 06 (output example and want to pass this data to view)
}
[HttpGet]
public ActionResult Index()
{
ViewBag.TagId = Session["RfidCurrent"]
return View();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.