如何处理错误,因为Value不能为null。参数名称:source

时间:2017-10-30 20:26:32

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

我在View中的下方视图代码位于

之下
@{
    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或空 enter image description here

如何处理此问题?

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

旧控制器代码

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上

enter image description here

1 个答案:

答案 0 :(得分:2)

考虑这个例子:

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,这可以通过将其声明置于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之外将声明为一次,然后在if / else语句中设置它的值。

由于我们在这里,我根本不会使用ViewBag,更不用说将DataTable带到Razor一侧了。我将使用viewmodels(在Microsoft ASP.NET MVC文档上阅读"Accessing Your Model's Data from a Controller"来查看其工作原理,特别是“强类型模型和@model关键字”部分,它们更清晰,更易于维护。

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

1)创建一个类,我们将其命名为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 */
}