4

I am new to asp.net, MVC. I am trying to populate my dropdownlist. I found following solution for that. How to write a simple Html.DropDownListFor()? I am trying BARAT's solution for that but facing error of NullReferenceException. Following 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: Object reference not set to an instance of an object.

Can Anyone please help me? I might be making small mistake but not able to fix it.

Community
  • 1
  • 1
dig123
  • 55
  • 1
  • 6

1 Answers1

2

Thanks LostDreamer for your comment. I made following changes in my code and it's working now. I don't know why Mode.CreditCardType was not working. In the reference, they have used the same thing but it is not working in my case. anyway following is 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")
            )%>
dig123
  • 55
  • 1
  • 6