我该如何处理错误,因为值不能为空。参数名称:来源

How can I handle error as Value cannot be null. Parameter name: source

我在视图中的下方视图代码如下

@{
    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();
}

写这篇文章时出现错误,因为当前上下文中不存在 StudentRequestTime。

如果我从控制器端返回 ViewBag.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 中获取数据,在这种情况下我该如何管理? 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];

在ViewSide

考虑这个例子:

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 语句。只有代码 within if 语句才能知道它的存在。

为了让我的示例工作,greenEyeColorFound 应该可以通过 both ifs 访问,这可以通过将它的声明放在两者之外来实现ifs:

// 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 之外 声明一次 ,然后只需在 if/else 语句中设置它的值即可。


既然我们已经开始了,我根本不会使用 ViewBag,更不用说让它携带 DataTables 到 Razor 端了。我会使用 viewmodels(阅读 "Accessing Your Model's Data from a Controller" Microsoft ASP.NET MVC 文档以了解它是如何工作的,特别是 "Strongly Typed Models and the @model Keyword" 部分),它更加清晰和可维护。

您可以通过以下步骤轻松重构现有代码以使用视图模型:

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

public class StudentRequestTimeViewModel
{
    public int StudentRequestId { get; set; }
    public string FromTime { get; set; }
    public string ToTime { get; set; }
}

2) 在您的控制器中,填充 List<StudentRequestTimeViewModel>:

var studentRequestTimes = new List<StudentRequestTimeViewModel>();
if (GetData.Tables[1].Rows.Count > 0 && GetData.Tables[0].Rows.Count > 0)
{
    // populate studentRequestTimes here        
}
// return the view, passing in studentRequestTimes as our model
return View(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 */
}