我有一个正在运行的模型和控制器,但我的模型没有在视图中显示任何数据。关于我做错了什么想法?
我有一个模型,我试图在其中传递几个变量。
我正在尝试使用Html.ViewData.Model。(此处为变量名)。即使我在模型中有硬设置值,我仍然会得到一个null异常。
任何想法
模型
public class PEOController : Controller
{
//Set default values
private string AvResult = "90";
private string BvResult = "T";
private string MvResult = "O";
private string rmNum = "Room";
// GET: PEO
public ActionResult PEO(string BvResult, string AvResult, string MvResult, string rmNum)
{
var model = new BACnetModel
{
AvRes = AvResult,
BvRes = BvResult,
MvRes = MvResult,
RmNum = rmNum
};
return View(model);
}
public ActionResult getRoomNumber(string roomNumber, ref uint BvInstance, ref uint AvInstance, ref uint MvInstance, out string rmNum)
{
switch (roomNumber)
{
case ("1B^1001^01"):
rmNum = "1B^1001^01";
BvInstance = 3000018;
AvInstance = 3000022;
MvInstance = 3000040;
break;
case ("1B^1002^01"):
rmNum = "1B^1002^01";
BvInstance = 3000020;
AvInstance = 3000023;
MvInstance = 3000042;
break;
default:
break;
}
rmNum = "Room";
var model = new BACnetModel
{
AvRes = AvResult,
BvRes = BvResult,
MvRes = MvResult,
RmNum = rmNum
};
return View(model);
}
}
控制器
@using Stuff
@using Microsoft.Ajax.Utilities
@model BACnetModel
@{
ViewBag.Title = "";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2> Stuff(PEO)</h2>
<p>
Stuff
</p>
<label>Select a Room: </label>
<!-- I need to add Code that will loop through the database and provide rooms.
I will need to provide a query that will be called on page load.-->
<form action="/PEO/getRoomNumber">
<select id="roomList">
<option value="1B^1001^01">1B^1001^01</option>
<option value="1B^1002^01">1B^1002^01</option>
</select><br/><br/>
<fieldset id="peoFieldset">
<legend>Room Results</legend>
<label for="roomNumber">Room Number:</label>
<input id="roomNumber" readonly="readonly"/>@Html.ViewData.Model.RmNum<br>
<label for="rmSetpoint">Room Setpoint: </label>
<input id="rmSetpoint" readonly="readonly" />@Html.ViewData.Model.AvRes<br>
<label for="rmCode">Room Code: </label>
<input id="rmCode" readonly="readonly" />@Html.ViewData.Model.BvRes<br>
<label for="rmOcc">Room Occupancy: </label>
<input id="rmOcc" readonly="readonly" />@Html.ViewData.Model.MvRes<br>
</fieldset>
</form>
<label>Occupancy Value</label>
<select id="occValue">
<option value="Occupied">Occupied</option>
<option value="Unoccupied">Unoccupied</option>
</select>
查看
var app = new FotoPrint();
function FotoPrint() {
this.init = function() {
this.im = new Image();
this.im.onload = function () {
this.canvas = document.getElementById('canvas');
this.ctx = this.canvas.getContext("2d");
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.strokeStyle = "black";
this.ctx.lineWidth = 2;
this.ctx.strokeRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.drawImage(this.im,0,0);
};
this.im.src = "Images/allison1.jpg";
};
};
app.init();
答案 0 :(得分:1)
我不确定您在视图中使用ViewData的原因。请使用标准模型绑定。在您的视图中尝试以下代码:
<fieldset id="peoFieldset">
<legend>Room Results</legend>
<label for="roomNumber">Room Number:</label>
<input id="roomNumber" readonly="readonly"/>@Model.RmNum<br>
<label for="rmSetpoint">Room Setpoint: </label>
<input id="rmSetpoint" readonly="readonly" />@Model.AvRes<br>
<label for="rmCode">Room Code: </label>
<input id="rmCode" readonly="readonly" />@Model.BvRes<br>
<label for="rmOcc">Room Occupancy: </label>
<input id="rmOcc" readonly="readonly" />@Model.MvRes<br>
</fieldset>
答案 1 :(得分:-1)
我得到了它的工作。
首先,我在任何方法之外的控制器中创建了一个模型实例。
private BACnetModel model = new BACnetModel();
一旦我这样做,我就会设置模型的值并将它们作为Action方法的一部分返回。
public ActionResult PEO()
{
var Room1 =
new SelectListItem {Text = "1B^1001^01", Value = "1B^1001^01"};
var Room2 =
new SelectListItem {Text = "1B^1002^01", Value = "1B^1002^01"};
roomList.Add(Room1);
roomList.Add(Room2);
ViewBag.RoomList = new SelectList(roomList, "Text", "Value");
model.RmNum = "";
model.AvRes = "";
model.BvRes = "";
model.MvRes = "";
return View(model);
}
然后我转到我的视图并将Model值放在视图中。
<fieldset id="peoFieldset">
<legend>Room Results</legend>
<label for="roomNumber">Room Number:</label>
<input id="roomNumber" readonly="readonly" value="@Model.RmNum" /><br>
<label for="rmSetpoint">Room Setpoint: </label>
<input id="rmSetpoint" readonly="readonly" value="@Model.AvRes" /><br>
<label for="rmCode">Room Code: </label>
<input id="rmCode" readonly="readonly" value="@Model.BvRes" /><br>
<label for="rmOcc">Room Occupancy: </label>
<input id="rmOcc" readonly="readonly" value="@Model.MvRes" /><br>
</fieldset>
像魅力一样!