简体   繁体   English

ASP.NET MVC | DropDownListFor给出错误的对象引用未设置为对象的实例

[英]ASP.NET MVC | DropDownListFor gives error Object reference not set to an instance of an object

i want to have a dropdown in my view. 我想在下拉菜单中查看。

but that dropdown should have categories loaded from database. 但该下拉菜单应具有从数据库加载的类别。

I am using Entity Framework Code First Approach in MVC 5. 我在MVC 5中使用实体框架代码优先方法。

Here is My 这是我的

Model. 模型。

public class CreateProductModel
{

        [Required]
        public string Name { get; set; }

        [Required]
        public int CategoryID { get; set; }
        public SelectList Categories { get; set; } 

        [MaxLength]
        public double Price { get; set; }

        public string Description { get; set; }


}

Controller: 控制器:

public ActionResult Index()
{
    var model = new ProductModel();
    model.CreateProductModel.Categories = new SelectList(_db.Categories, "CategoryID", "Name", 1);
    return View(model);
}

View: 视图:

                <div class="form-group">
                    @Html.LabelFor(model => model.CreateProductModel.CategoryID, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-10">
                        @Html.DropDownListFor(model => model.CreateProductModel.CategoryID, new SelectList(Model.CreateProductModel.Categories, "CategoryID", "Name", 1), "Please Select Category");
                    </div>
                </div>

I am getting this error.. 我收到此错误。 在此处输入图片说明

Initialize CreateProductModel property: 初始化CreateProductModel属性:

var model = new ProductModel();
model.CreateProductModel = new CreateProductModel();
model.CreateProductModel.Categories = new SelectList(_db.Categories, "CategoryID", "Name", 1);

暂无
暂无

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

相关问题 ASP.NET MVC对象引用未设置为对象的实例 - ASP.NET MVC Object reference not set to an instance of an object Asp.Net MVC4&#39;对象引用未设置为对象的实例&#39; - Asp.Net MVC4 'Object reference not set to an instance of an object' ASP.NET MVC3 - “对象引用未设置为对象的实例”错误 - ASP.NET MVC3 - “Object reference not set to an instance of an object” error NullReferenceException:Object 引用未设置为 ASP.NET MVC 中的 object 错误的实例 - NullReferenceException: Object reference not set to an instance of an object error in ASP.NET MVC ASP.NET MVC“'对象引用未设置为 object 的实例。'”错误 - ASP.NET MVC "'Object reference not set to an instance of an object.'" Error Object 参考未设置为 object 的实例? (异常错误~ASP.NET MVC) - Object Reference not set to an instance of an object? (Exception error ~ ASP.NET MVC) 尝试使用 ASP.NET MVC 上传 PDF 文件导致错误“对象引用未设置为对象的实例” - Trying to upload a PDF file using ASP.NET MVC is causing an error "Object reference not set to an instance of an object" 发布我的 asp.net web 应用程序会给出“对象引用未设置为 object 的实例。” - Publishing my asp.net web application gives 'Object reference not set to an instance of an object.' ASP.NET MVC对象参考错误 - ASP.NET MVC Object reference error “对象引用未设置为对象的实例。”嵌套列表ASP.NET MVC LINQ实体框架 - “Object reference not set to an instance of an object.” Nesting Lists ASP.NET MVC LINQ Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM