Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在MVC中使用自定义模型_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 如何在MVC中使用自定义模型

Asp.net mvc 如何在MVC中使用自定义模型,asp.net-mvc,Asp.net Mvc,我在MVC中创建了一个自定义模型,其中我将3个表作为列表传递。在视图中,我正在获取这些细节。但在视图中,我得到的对象引用并没有设置为对象的实例。 我是MVC的新手,有人能帮我吗。我不知道我哪里做错了 我的MVC控制器: public ActionResult Index() { var adminModel = new AdminModel(); return View(adminModel); } 我的自定义模型代码: public class AdminModel {

我在MVC中创建了一个自定义模型,其中我将3个表作为列表传递。在视图中,我正在获取这些细节。但在视图中,我得到的对象引用并没有设置为对象的实例。 我是MVC的新手,有人能帮我吗。我不知道我哪里做错了

我的MVC控制器:

public ActionResult Index()
{
   var adminModel = new AdminModel();
   return View(adminModel);
}
我的自定义模型代码:

public class AdminModel
{
    public List<Notification> Notifications { get; set; }            
    public List<Places> Places { get; set; }
}
类文件:

 public class AdminModel
    {

        public List<Notification> Notifications { get; set; } = new List<Notification>();
        public List<Places> Places { get; set; } = new List<Places>();
    }
公共列表通知{get;set;}=新列表;
公共列表位置{get;set;}=新列表

在将模型传递到视图之前,未填充模型。您需要这样称呼:

public ActionResult Index()
{
    var adminModel = new AdminModel();

    adminModel.Notifications = new List<Notifications>();
    // Create a new notification [yourCreatedNotification]... then add it to the list
    adminModel.Notifications.Add(yourCreatedNotification);

     return View(adminModel);
}
通知在您的AdminModel类中没有默认值,因此,当您将新的AdminModel对象发送到视图时,该属性为null,您无法调用。请使用它

解决方案取决于您试图实现的目标,但如果您希望从避免异常开始,则可以通过初始化属性向其添加默认值

public class AdminModel
    {
        public List<Notification> Notifications { get; set; } = new List<Notification>()            
        public List<Places> Places { get; set; }
    }
如果您不想在类中更改,可以在将其发送到视图之前设置其值:

public ActionResult Index()
{
   var adminModel = new AdminModel();
   adminModel.Notifications = new List<Notification>();
   return View(adminModel);
}

您可能应该初始化模型上的集合,或者测试视图中的集合是否为null

var adminModel = new AdminModel
{
   Notifications = new List<Notification>(),
   Places = new List<Places>()
};


实际上,我没有将数据传递给在自定义模型中创建的列表项。因此,新控制器代码将为:

public ActionResult Index()
        {
            var adminModel = new AdminModel {

                Notifications = db.Notifications.ToList(),
                Places = db.Places.ToList()

            };
            return View(adminModel);
        }
和自定义模型类文件:

 public class AdminModel
    {

        public List<Notification> Notifications { get; set; } = new List<Notification>();
        public List<Places> Places { get; set; } = new List<Places>();
    }

它解决了我的问题,但我的通知表中有1个数据,在视图中显示0值!如何解决此问题?请与我们分享您的新代码或询问其他问题,以便我们能够帮助您。
public class AdminModel
{
    public List<Notification> Notifications { get; set; }            
    public List<Places> Places { get; set; }

   public AdminModel()
   {
      Notifications = new List<Notification>();
      Places = new List<Places>();
   }
}
public ActionResult Index()
        {
            var adminModel = new AdminModel {

                Notifications = db.Notifications.ToList(),
                Places = db.Places.ToList()

            };
            return View(adminModel);
        }
 public class AdminModel
    {

        public List<Notification> Notifications { get; set; } = new List<Notification>();
        public List<Places> Places { get; set; } = new List<Places>();
    }