MVC DropDownListFor - NullReferenceException由usercode解除

时间:2012-11-18 15:09:32

标签: asp.net asp.net-mvc

我是asp.net,MVC的新手。我正在尝试填充我的下拉列表。我找到了以下解决方案。 How to write a simple Html.DropDownListFor()?我正在尝试BARAT的解决方案,但面临NullReferenceException的错误。以下是我的代码。

<%: 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:对象引用未设置为对象的实例。

任何人都可以帮助我吗?我可能会犯小错误但无法修复它。

1 个答案:

答案 0 :(得分:2)

感谢LostDreamer的评论。我在代码中进行了以下更改,现在正在运行。我不知道为什么Mode.CreditCardType无效。在参考文献中,他们使用了相同的东西,但它在我的情况下不起作用。无论如何,以下是解决方案。

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