MVC5模型在视图中回归空虚

时间:2015-07-23 18:59:36

标签: asp.net-mvc view model asp.net-mvc-5

我有一个单一的模型,我试图在其中传递几个变量。

我正在尝试使用Html.ViewData.Model。(此处为变量名)。即使我在模型中有硬设置值,我仍然会得到一个null异常。

任何想法

模型

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; } }
    }
}

在下面添加了已编辑的控制器和查看操作。我通过声明模型得到了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="/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>

1 个答案:

答案 0 :(得分:1)

是否会返回视图的以下操作?

// GET: CCEC
public ActionResult CCEC()
{
    return View();
}

如果是,则将模型设置为null,这样您将获得空引用异常。您需要至少给它一个空白模型或在视图中测试null。

// GET: CCEC
public ActionResult CCEC()
{
    return View(new BACnetModel());
}