MVC DropDownListFor - NullReferenceException failed with custom code

I am new to asp.net, MVC. I am trying to populate my dropdownlist. I found the following solution for this. How to write a simple Html.DropDownListFor ()? I am trying to find a BARAT solution for this, but is encountering a NullReferenceException error. Below is my code.

<%: Html.DropDownListFor(model => model.CreditCardType, new SelectList( new List<Object>{ new { value = 0, text="VISA"}, new { value = 1, text="Master"}, new { value = 2, text="Debit"}}, "value", "text", Model.CreditCardType) )%> 

ErrorDetail: the reference to the object is not installed in the instance of the object.

Can anyone help me out? I could make a small mistake, but could not fix it.

+4
source share
1 answer

Thanks to LostDreamer for your comment. I made the following changes to my code, and now it works. I do not know why Mode.CreditCardType did not work. In the link they used the same thing, but in my case this does not work. anyway the following solution.

  model.CreditCardType,
         new SelectList (
             new List {
             new {value = 0, text = "VISA"},
             new {value = 1, text = "Master"},
             new {value = 2, text = "Debit"}},
             "value",
             "text", 
             "VISA")
             )%>
+2
source

Source: https://habr.com/ru/post/1446769/


All Articles