简体   繁体   English

当我想通过模型访问它时,模型上的 MVC Null 异常在 ViewModel 中传递给 View。 不是@Html

[英]MVC Null exception on Model passed in ViewModel to View when I want to acces it via Model. not @Html

I am having problem in MVC with my Model which is passed in ViewModel to View.我在 MVC 中遇到了在 ViewModel 中传递给 View 的模型的问题。 I have database with several tables on which I called entity framework to create models.我有几个表的数据库,我在上面调用了实体框架来创建模型。

This is my ViewModel:这是我的视图模型:

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();

        }
    }
}

I create few properties for my tables and I initialize some of them.我为我的表创建了几个属性,并初始化了其中的一些。 Then in my controller i call my ViewModel class so it should create instance:然后在我的控制器中我调用我的 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);

In my View I get the ViewModel, then I want to use it.在我的视图中,我得到了 ViewModel,然后我想使用它。 I call DropDownListFor:我打电话给 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>

I want to populate the dropdown options with data from namtar_studyrooms table/model but can't.我想用来自 namtar_studyrooms 表/模型的数据填充下拉选项,但不能。 It is working for the first labmda expression which is taking from @Html (as model.reports while model is my viewmodel) but the next lamda I use with Model.Namtar_studyrooms.Select( does not work and I am getting null exception on Model. I looked on the internet and it seems I have everything as everyone else and I cant seem to find what is wrong它适用于从@Html 获取的第一个 labmda 表达式(作为 model.reports,而模型是我的视图模型),但我与 Model.Namtar_studyrooms.Select( 一起使用的下一个 lamda 不起作用,我在模型上遇到空异常。我在互联网上查看,似乎我和其他人一样拥有一切,但似乎无法找到问题所在

Add the dropdown collection to ViewBag.将下拉集合添加到 ViewBag。

In Controller;在控制器中;

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

In View;在视图中;

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

Hope Helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM