Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
C# 获取错误,例如;对象引用未设置为对象的引用;在Asp.NETMVC中 我有模型课_C#_Asp.net Mvc - Fatal编程技术网

C# 获取错误,例如;对象引用未设置为对象的引用;在Asp.NETMVC中 我有模型课

C# 获取错误,例如;对象引用未设置为对象的引用;在Asp.NETMVC中 我有模型课,c#,asp.net-mvc,C#,Asp.net Mvc,这是怎么回事 "Object reference not set to reference of the object" 我从模型类调用List并返回列表元素的“count”,它应该返回列表中元素的数量,但返回null引用。任何人请帮帮我,我如何修复它 Model.GetTimesheetDetails为null。您需要使用new关键字创建一个实例。问题是您引用GetTimesheetDetails而没有初始化它,所以,当您执行GetTimesheetDetails.Cou

这是怎么回事

          "Object reference not set to reference of the object"

我从模型类调用List并返回列表元素的“count”,它应该返回列表中元素的数量,但返回null引用。任何人请帮帮我,我如何修复它

Model.GetTimesheetDetails
null
。您需要使用
new
关键字创建一个实例。

问题是您引用
GetTimesheetDetails
而没有初始化它,所以,当您执行
GetTimesheetDetails.Count
GetTimesheetDetails
时,您并没有初始化模型数据并返回带有空模型的视图,这就是为什么它会给您错误,因为对象未实例化

您必须像这样实例化它:

public Employer Controller
{
   public ActionResult Timesheet()
   {
       GetTimesheetList model = new GetTimesheetList();
       model.GetTimesheetDetails = new List<TimesheetModel>();
       return View(model);
   }
}
公共雇主控制员
{
公共行动结果时间表()
{
GetTimesheetList模型=新建GetTimesheetList();
model.GetTimesheetDetails=新列表();
返回视图(模型);
}
}

更新控制器方法,如下所述:

public Employer Controller
{
    public ActionResult Timesheet()
    {
        return View(new GetTimesheetList{
        GetTimesheetDetails = new List<TimesheetModel>()            
        });
    }
}
公共雇主控制员
{
公共行动结果时间表()
{
返回视图(新GetTimesheetList{
GetTimesheetDetails=新列表()
});
}
}

注意:这将返回类的新实例
GetTimesheetList
。它不会给您任何错误,但不会通过循环,因为它没有任何数据

您并没有将模型返回到您的视图,因为您的视图接受模型

您的视图已经定义了这一点

@model Project1.Models.GetTimesheetList
在视图中,您尝试访问此模型。它试图使用它的第一行是
Model.GetTimesheetDetails.Count
,因为没有传递任何模型
Model.GetTimesheetDetails
为null,因此它引发异常

您将需要向视图传递一个模型,例如

public Employer Controller
{
    public ActionResult Timesheet() 
    {
        // get model from somewhere;
        return View(model);
    }
}
如果您需要传递一个空模型,这将非常有用

public ActionResult Timesheet() 
{
    var model = new GetTimesheetList();
    model.GetTimesheetDetails = new List<TimesheetModel>();
    return View(model);
}
public ActionResult时间表()
{
var model=new GetTimesheetList();
model.GetTimesheetDetails=新列表();
返回视图(模型);
}
但我怀疑你的情况会是这样,因为有了这个,你的for循环将被跳过
Model.GetTimesheetDetails.Count
现在不会抛出错误,而是为零并跳过循环。

我需要获取列表中元素的数量,那么解决方案是什么@Mike Norgate我需要从列表中获取元素的数量,需要添加记录并保存在数据库中,解决方案是什么?你能发布返回视图的控制器方法吗?看看我的答案。希望它能帮助你解决你的问题。哦,谢谢。。!!我尝试过用您的答案得到错误,比如“传入字典的模型项的类型为'Project1.Models.GetTimesheetList',但此字典需要'Project1.Models.TimesheetModel'类型的模型项。”@Ehsan Sajjadin您需要将此模型设置为问题帖子中的模型:model Project1.Models.GetTimesheetList而不是model Project1.Models.TimesheetModelsorry,得到的错误与传递到字典的模型项的类型为“Project1.Models.GetTimesheetList”,但此字典需要类型为“Project1.Models.TimesheetModel”的模型项。“@spidercode右键单击方法名称时间表,然后选择
转到查看
。并检查它接受什么对象?如果是
@model Project1.Models.TimesheetModel
,则返回时间表模型的新实例。像
返回视图(new List())
是的,这是正确的,我的模型返回空的,我需要为列表中的所有元素创建文本框,我该怎么做@亚西尔
@model Project1.Models.GetTimesheetList
public Employer Controller
{
    public ActionResult Timesheet() 
    {
        // get model from somewhere;
        return View(model);
    }
}
public ActionResult Timesheet() 
{
    var model = new GetTimesheetList();
    model.GetTimesheetDetails = new List<TimesheetModel>();
    return View(model);
}