【问题标题】:MVC Null exception on Model passed in ViewModel to View when I want to acces it via Model. not @Html当我想通过模型访问它时,模型上的 MVC Null 异常在 ViewModel 中传递给 View。不是@Html
【发布时间】:2016-05-12 15:33:23
【问题描述】:

我的模型在 MVC 中出现问题,该模型在 ViewModel 中传递给 View。我有几个表的数据库,我在这些表上调用实体框架来创建模型。

这是我的视图模型:

namespace BP_final.ViewModels
{
     public class AllDBModels
    {
        public Models.Graph AllGraphs { get; set; }
        public IEnumerable<NAMTAR_STUDYROOMS> Namtar_studyrooms { get; set; }
        public IEnumerable<NAMTAR_STATS_STUDYROOMS> Namtar_stats_studyrooms { get; set; }
        public List<bp_data> bp_data { get; set; }
        public Models.Report reports { get; set; }

        public AllDBModels()
        {
            Namtar_studyrooms = new List<NAMTAR_STUDYROOMS>();
            reports = new Models.Report() { RoomNumber = "1"};
            AllGraphs = new Models.Graph();

        }
    }
}

我为我的表创建了一些属性并初始化了其中的一些。 然后在我的控制器中我调用我的 ViewModel 类,所以它应该创建实例:

public ActionResult Index()
    {
        AllDBModels allModels = new AllDBModels();
        allModels.AllGraphs = new Graph();
        allModels.AllGraphs.Charts = new List<Highcharts>();

        ChartsModel model = new ChartsModel();
        model.Charts = new List<Highcharts>();
...
...

return View(allModels);

在我的视图中,我得到了 ViewModel,然后我想使用它。我打电话给 DropDownListFor:

@model BP_final.ViewModels.AllDBModels

@{

ViewBag.Title = "Create";

}
<h2>Create</h2>

@using (Html.BeginForm()) 
...
...

    <div class="form-group">
        @Html.LabelFor(model => model.reports.RoomNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10 ">
            @Html.DropDownListFor(model => model.reports.RoomNumber, new SelectList(
/*this fails*/   Model.Namtar_studyrooms.Select(x => new SelectListItem{ Value = x.ID.ToString(), Text = x.NAME }), "Value", "Text", 2
            ), new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.reports.Graph, "", new { @class = "text-danger" })
        </div>
    </div>

我想使用 namtar_studyrooms 表/模型中的数据填充下拉选项,但不能。它适用于从@Html 获取的第一个labmda 表达式(作为model.reports,而model 是我的viewmodel),但是我与Model.Namtar_studyrooms.Select(一起使用的下一个lamda 不起作用,并且我在Model 上遇到空异常。我在网上查了一下,似乎我和其他人一样拥有一切,但我似乎找不到问题所在

【问题讨论】:

  • 您显示的代码不会抛出该错误,尽管您显示的内容没有意义,因为Namtar_studyrooms 是一个空列表,不会显示任何选项。在SelectList 构造函数中添加最后一个参数(2)是没有意义的(它被忽略了,因为它的reports.RoomNumber 的值决定了选择的内容。

标签: c# asp.net asp.net-mvc viewmodel


【解决方案1】:

将下拉集合添加到 ViewBag。

在控制器中;

public ActionResult Index()
    {
    AllDBModels allModels = new AllDBModels();
    allModels.AllGraphs = new Graph();
    allModels.AllGraphs.Charts = new List<Highcharts>();

    ChartsModel model = new ChartsModel();
    model.Charts = new List<Highcharts>();
    ViewBag.Rooms = db.namtar_studyrooms.ToList() //the collection here (you should changeit, just an example)
}

在视图中;

 @Html.DropDownListFor(model => model.reports.RoomNumber, new SelectList(ViewBag.Rooms, "RoomID", "RoomNameForDisplay"), "Select Room for default", new { @class = "form-control" })

希望有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多