1

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.'

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.

@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).

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>'

And the message:

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

2
  • Obvious candidates are ViewBag.Product and ViewBag.Product.Documents[0]. Double check them. Commented Jan 15, 2013 at 15:32
  • After removing the entire ternary expression the same exception is still thrown. I can actually empty out the loop and end up with the same error. Commented Jan 15, 2013 at 16:07

2 Answers 2

2

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.

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

ViewBag.Product.Documents = something.GetProductDocuments.ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking it would be something to do with the source not being available any more but I couldn't hunt down the issue. Adding ToList did not work.
What does your code look like that is populating the ViewBag Product and Product.Documents properties?
1

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

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.

1 Comment

When passing the collection through a ViewModel I get the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.