MVC5 模型在视图中返回 Null

MVC5 Model Is coming back Null in View

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

我正在尝试使用 Html.ViewData.Model。(此处为变量名)。即使我在模型中有 hardset 值,我仍然收到 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; } }
    }
}

在下面添加了已编辑的控制器和视图操作。我通过声明模型消除了空值,但我仍然没有在我的视图中看到值。

控制器

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>

返回视图的是以下操作吗?

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

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

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