Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 我如何处理错误,因为值不能为null。参数名称:源_Asp.net Mvc_Asp.net Mvc 4_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 我如何处理错误,因为值不能为null。参数名称:源

Asp.net mvc 我如何处理错误,因为值不能为null。参数名称:源,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 3,视图中的“我的下视图”代码如下所示 @{ ViewBag.Title = "Student Dashboard"; var StudentRequestTimedt = ViewBag.StudentRequestTime as DataTable; if (StudentRequestTimedt != null) { var StudentRequestTime = StudentRequestTimedt.AsEnumerable()

视图中的“我的下视图”代码如下所示

@{
    ViewBag.Title = "Student Dashboard";    
    var StudentRequestTimedt = ViewBag.StudentRequestTime as DataTable;
    if (StudentRequestTimedt != null)
    {
        var StudentRequestTime = StudentRequestTimedt.AsEnumerable().Select(t => new
        {
            StudentRequestId = t.Field<int>("StudentRequestId"),
            FromTime = t.Field<string>("FromTime"),
            ToTime = t.Field<string>("ToTime"),
        }).ToList();
    }
    else
    { var StudentRequestTime = ""; }   
}

if (StudentRequestTime != "")
{
   var StudentRequestTimecount = StudentRequestTime.Where(d => d.StudentRequestId == StudentRequestId).ToList();
}
请同时查看下图,我在这里获取多个viewbag中的数据。在这种情况下,我如何管理?var StudentRequestTime为null或为空

我如何处理这个问题?

更新的代码解决了我的问题

控制器的旧代码

if (GetData.Tables[1].Rows.Count > 0 && GetData.Tables[0].Rows.Count > 0)
{
  ViewBag.StudentRequestTime = GetData.Tables[1];
 }
 else
 {
   ViewBag.StudentRequestTime = null;
 }
return View();
ViewBag.StudentRequestTime = GetData.Tables[1];
控制器的新代码

if (GetData.Tables[1].Rows.Count > 0 && GetData.Tables[0].Rows.Count > 0)
{
  ViewBag.StudentRequestTime = GetData.Tables[1];
 }
 else
 {
   ViewBag.StudentRequestTime = null;
 }
return View();
ViewBag.StudentRequestTime = GetData.Tables[1];
在视图侧

考虑一下这个例子:

if (eyeColor == EyeColor.Green)
{
    // greenEyeColorFound has been declared *in this if statement*,
    // so it only exists *within this if statement*
    var greenEyeColorFound = true;
}

// this will fail. greenEyeColorFound was declared *in the first if statement*,
// how can the if statement below be aware of it's existence?
if (greenEyeColorFound == true)
{       
    Debug.WriteLine("Found a person with green eyes!");
}
greenEyeColorFound
在本地作用域为第一条if语句。只有if语句中的代码才能知道它的存在

为了让我的示例发挥作用,
greenEyeColorFound
应该可以被两个
if
s访问,这可以通过将其声明放在两个
if
s之外来实现:

// this is now declared *outside* of the two if statements,
// so both are now aware of it and can access it's value.
var greenEyeColorFound = false;
if (eyeColor == EyeColor.Green)
{
    greenEyeColorFound = true;
}

// presto, this now works
if (greenEyeColorFound == true)
{       
    Debug.WriteLine("Found a person with green eyes!");
}
这正是您在
StudentRequestTime
中遇到的问题。在
if
s之外声明一次,然后在if/else语句中设置它的值


既然我们在使用它,我就根本不会使用
ViewBag
,更不用说让它把
DataTable
s带到剃须刀边了。我会使用viewmodels(请阅读Microsoft ASP.NET MVC文档,了解其工作原理,特别是“强类型模型和
@model
关键字”一节),它更简洁、更易于维护

您可以通过以下步骤轻松重构现有代码以使用viewmodels:

1) 创建一个类,我们将其命名为
StudentRequestTimeViewModel

public class StudentRequestTimeViewModel
{
    public int StudentRequestId { get; set; }
    public string FromTime { get; set; }
    public string ToTime { get; set; }
}
2) 在控制器中,填充
列表

var studentRequestTimes=new List();
if(GetData.Tables[1].Rows.Count>0&&GetData.Tables[0].Rows.Count>0)
{
//在此处填充studentRequestTimes
}
//返回视图,将studentRequestTimes作为我们的模型传入
返回视图(studentRequestTimes);
3) 然后,您的剃须刀变成:

/* your model is declared as "@model",
   but is accessed as "Model".         */
@model List<StudentRequestTimeViewModel>

@if (Model != null && Model.Count > 0)
{
    /* your List<StudentRequestTimeViewModel> Model is not null or empty */
    foreach(var studentRequestTime in Model)
    {
        <p>Student with ID @studentRequestTime.StudentRequestId is here.</p>
    }
}
else
{
    /* your List<StudentRequestTimeViewModel> Model is null or empty */
}
/*您的模型声明为“@model”,
但作为“模型”访问*/
@模型列表
@if(Model!=null&&Model.Count>0)
{
/*您的列表模型不为null或空*/
foreach(模型中的var studentRequestTime)
{
ID为@studentRequestTime.StudentRequestId的学生在这里

} } 其他的 { /*您的列表模型为null或为空*/ }
考虑以下示例:

if (eyeColor == EyeColor.Green)
{
    // greenEyeColorFound has been declared *in this if statement*,
    // so it only exists *within this if statement*
    var greenEyeColorFound = true;
}

// this will fail. greenEyeColorFound was declared *in the first if statement*,
// how can the if statement below be aware of it's existence?
if (greenEyeColorFound == true)
{       
    Debug.WriteLine("Found a person with green eyes!");
}
greenEyeColorFound
在本地作用域为第一条if语句。只有if语句中的代码才能知道它的存在

为了让我的示例发挥作用,
greenEyeColorFound
应该可以被两个
if
s访问,这可以通过将其声明放在两个
if
s之外来实现:

// this is now declared *outside* of the two if statements,
// so both are now aware of it and can access it's value.
var greenEyeColorFound = false;
if (eyeColor == EyeColor.Green)
{
    greenEyeColorFound = true;
}

// presto, this now works
if (greenEyeColorFound == true)
{       
    Debug.WriteLine("Found a person with green eyes!");
}
这正是您在
StudentRequestTime
中遇到的问题。在
if
s之外声明一次,然后在if/else语句中设置它的值


既然我们在使用它,我就根本不会使用
ViewBag
,更不用说让它把
DataTable
s带到剃须刀边了。我会使用viewmodels(请阅读Microsoft ASP.NET MVC文档,了解其工作原理,特别是“强类型模型和
@model
关键字”一节),它更简洁、更易于维护

您可以通过以下步骤轻松重构现有代码以使用viewmodels:

1) 创建一个类,我们将其命名为
StudentRequestTimeViewModel

public class StudentRequestTimeViewModel
{
    public int StudentRequestId { get; set; }
    public string FromTime { get; set; }
    public string ToTime { get; set; }
}
2) 在控制器中,填充
列表

var studentRequestTimes=new List();
if(GetData.Tables[1].Rows.Count>0&&GetData.Tables[0].Rows.Count>0)
{
//在此处填充studentRequestTimes
}
//返回视图,将studentRequestTimes作为我们的模型传入
返回视图(studentRequestTimes);
3) 然后,您的剃须刀变成:

/* your model is declared as "@model",
   but is accessed as "Model".         */
@model List<StudentRequestTimeViewModel>

@if (Model != null && Model.Count > 0)
{
    /* your List<StudentRequestTimeViewModel> Model is not null or empty */
    foreach(var studentRequestTime in Model)
    {
        <p>Student with ID @studentRequestTime.StudentRequestId is here.</p>
    }
}
else
{
    /* your List<StudentRequestTimeViewModel> Model is null or empty */
}
/*您的模型声明为“@model”,
但作为“模型”访问*/
@模型列表
@if(Model!=null&&Model.Count>0)
{
/*您的列表模型不为null或空*/
foreach(模型中的var studentRequestTime)
{
ID为@studentRequestTime.StudentRequestId的学生在这里

} } 其他的 { /*您的列表模型为null或为空*/ }

这是因为
StudentRequestTime
是局部作用域(仅存在于第一个if语句中)^^。在
之外定义
StudentRequestTime
如果
我应该在语句之外写什么??var StudentRequestTime=“”@Xtremcool查看我的更新答案。这是因为
StudentRequestTime
是局部作用域(仅存在于第一个if语句中)^^。在
之外定义
StudentRequestTime
如果
我应该在语句之外写什么??var StudentRequestTime=“”@Xtremcool查看我的更新答案。请查看我添加了实际视图布局的mu更新问题我的答案是针对您的问题“值不能为null时如何处理错误”-更新后的问题是一个全新的问题,因此您应该在新帖子中提出该问题。另外,从截图上看,看起来您没有遵循您的问题和答案评论中提出的任何建议。您要求我调用与StudentRequestTimeViewModel相关的模型,但我需要将模型称为StudentRequest。我的问题是我应该如何处理我的viewbag,以及我应该分配什么,以便它能够在流程中工作,你让我创建模型并做以下事情,所以我上传了我的代码图像,以便你理解我的实际意思,并更新你的答案,就像我在问题中写的那样,将你标记为答案,另外,您告诉我的方式对我很有帮助,所以请不要删除,只添加它。请查看我添加了其他内容的mu更新问题我的实际视图布局我的答案是针对您的问题“我如何处理错误,因为值不能为空”-更新后的问题是一个新问题,因此您应该在新的p中提出该问题