Reputation: 335
I have the a single model that I am trying to pass with a couple variables inside it.
I am trying to use Html.ViewData.Model.(Variable name here). I keep getting a null exception even though I have hardset values in the model.
Any ideas
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
namespace stuff
{
public class BACnetModel
{
private string _rmNum = "Room Number";
public string RmNum { get { return _rmNum; } set{_rmNum = value;} }
private string _avRes = "70";
public string AvRes { get { return _avRes; } set {_avRes = value;} }
private string _bvRes = "T";
public string BvRes { get { return _bvRes; } set { _bvRes = value; } }
private string _mvRes = "O";
public string MvRes { get { return _mvRes; } set { _mvRes = value; } }
}
}
Added edited controller and view actions below. I've got the null gone by declaring the model but I still am not seeing values in my view.
Controller
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);
}
}
View
@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="/BACnetIntegration/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"/>@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>
</form>
<label>Occupancy Value</label>
<select id="occValue">
<option value="Occupied">Occupied</option>
<option value="Unoccupied">Unoccupied</option>
</select>
Upvotes: 0
Views: 1883
Reputation: 759
Is it the following action that is returning the view?
// GET: CCEC
public ActionResult CCEC()
{
return View();
}
If so then the model will be set to null so you will get a null reference exception. You need to give it a blank model at least or test for null in the view.
// GET: CCEC
public ActionResult CCEC()
{
return View(new BACnetModel());
}
Upvotes: 1