-1

Hey is it possible to set Dictionary<int, double> values in view?

For now I'm tried one example that looks like this:

@for (int i = 0; i < Model.Certificates.Count; i++)
{
    <tr>
        <td>@Model.Certificates[i].Text</td>
        <td><input type="hidden" name="@Model.Dictionary.ElementAt(i).Key" value="@Model.Certificates[i].Value" /></td>
        <td><input name="@Model.Dictionary.ElementAt(i).Value"/></td>                                           
    </tr>
}

But I'm getting this kind of error:

NullReferenceException: Object reference not set to an instance of an object.

Nikas Žalias
  • 1,456
  • 19
  • 43
  • 1
    The problem is that the Model you pass to your view has a Dictionary, which is null. So you can't access anything on this. You should find out why it is null and then you should take care of this in your view by checking if it is not null and only if it is not to access any element on this. – Christos Jul 01 '16 at 14:30
  • 2
    Is the question 'Is it possible to set a Dictionary value when posting values from a view?' or is the question 'Why am I getting an object reference exception when my view is rendering?' – Luke Jul 01 '16 at 14:34
  • You need to give us some more code. Where do you Initialize your `Dictionary`-Property from your Model? – brothers28 Jul 01 '16 at 14:35

1 Answers1

0

Is the dictionary initialized? If you aren't declaring the dictionary as a new dictionary somewhere you won't be able to get the Count of it, which is why it is giving you a null reference error.

  • I have initialized dictionary, but then my view thows me an error _ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index_ so as I understand this error means that my dictionary is not the size as it should be, because in my view I made loop where I want to set my dictionary values – Nikas Žalias Jul 04 '16 at 08:57