ASP.NET MVC应用程序不喜欢我的for循环

时间:2022-11-14 04:13:30

I rewrote a portion of my MVC application the other day and now I'm getting a RuntimeBinderException stating 'Cannot perform runtime binding on a null reference.'

我前几天重写了一部分MVC应用程序,现在我得到一个RuntimeBinderException,声明“无法对空引用执行运行时绑定”。

I am assigning the ViewBag variable AvailableDocuments a collection (Dictionary) in the Controller and iterating through it in the View. I can make it through the first loop just fine, but when the code reaches the exit point of the second for loop it throws this exception. I have rewritten this a few different ways and always run into the same issue. The only way that I can get rid of this exception is to remove the second/third loops from this View. Also, I have used the debugger to verify that I am not receiving any null values in the collection and that the first loop isn't (for no reason) performing changes to this collection.

我在View中为ViewBag变量AvailableDocuments分配一个集合(Dictionary),并在View中迭代它。我可以通过第一个循环完成它,但是当代码到达第二个for循环的出口点时,它会抛出此异常。我已经用几种不同的方式重写了这一点,并且总是遇到同样的问题。我可以摆脱这个异常的唯一方法是从这个视图中删除第二个/第三个循环。此外,我使用调试器来验证我没有在集合中接收任何空值,并且第一个循环不是(无缘无故)对此集合执行更改。

@foreach (KeyValuePair<int, string> doc in ViewBag.AvailableDocuments)
{
    <option value="@doc.Key" @((ViewBag.Product.Documents[0].OriginalDocID == doc.Key) ? "selected" : "")>@doc.Value</option>
}

This is the for loop that is repeated three times (to fill three dropdowns). The only thing that is changing is the index of Documents (which is not null).

这是重复三次的for循环(填充三个下拉列表)。唯一改变的是Document的索引(不是null)。

Line 62: <select name="Doc1" style="width:300px;">
Line 63: <option value="-1">Don't Display a Document</option>
Line 64: @foreach (KeyValuePair<int, string> doc in ViewBag.AvailableDocuments)<--BREAK LINE
Line 65: {
Line 66: <option value="@doc.Key" @((ViewBag.Product.Documents[0].OriginalDocID == doc.Key) ? "selected" : "")>@doc.Value</option>'

第62行:

And the message:

并且消息:

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

异常详细信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法对空引用执行运行时绑定

2 个解决方案

#1


1  

Are you reading the data in ViewBag.Product.Documents from a database or a webservice? If so, I'm guessing you are running into an issue with lazy loading (the data isn't being requested until it is needed). The exception is occurring because when the data is being requested, the source is no longer available.

您是从数据库或Web服务中读取ViewBag.Product.Documents中的数据吗?如果是这样,我猜你正在遇到延迟加载的问题(在需要之前不会请求数据)。发生异常是因为在请求数据时,源不再可用。

In your code behind, add a ToList() on the assignment and see if that fixes the problem.:

在你的代码后面,在赋值中添加一个ToList(),看看是否能修复问题:

ViewBag.Product.Documents = something.GetProductDocuments.ToList();

#2


0  

What happens if you create a collection of pocos with the desired data, could be as simple as this

如果您创建具有所需数据的pocos集合会发生什么,可能就像这样简单

public class OptionItem
{
   public int Id {get; set;}
   public string Text {get; set;}
}

and add it to a collection on the view model and pass that to the view? This way you know that all of the data is their before your going to your view. I would personally avoid using viewbag for something like this.

并将其添加到视图模型上的集合并将其传递给视图?通过这种方式,您可以在进入视图之前知道所有数据。我个人会避免使用viewbag这样的东西。

#1


1  

Are you reading the data in ViewBag.Product.Documents from a database or a webservice? If so, I'm guessing you are running into an issue with lazy loading (the data isn't being requested until it is needed). The exception is occurring because when the data is being requested, the source is no longer available.

您是从数据库或Web服务中读取ViewBag.Product.Documents中的数据吗?如果是这样,我猜你正在遇到延迟加载的问题(在需要之前不会请求数据)。发生异常是因为在请求数据时,源不再可用。

In your code behind, add a ToList() on the assignment and see if that fixes the problem.:

在你的代码后面,在赋值中添加一个ToList(),看看是否能修复问题:

ViewBag.Product.Documents = something.GetProductDocuments.ToList();

#2


0  

What happens if you create a collection of pocos with the desired data, could be as simple as this

如果您创建具有所需数据的pocos集合会发生什么,可能就像这样简单

public class OptionItem
{
   public int Id {get; set;}
   public string Text {get; set;}
}

and add it to a collection on the view model and pass that to the view? This way you know that all of the data is their before your going to your view. I would personally avoid using viewbag for something like this.

并将其添加到视图模型上的集合并将其传递给视图?通过这种方式,您可以在进入视图之前知道所有数据。我个人会避免使用viewbag这样的东西。