错误类型的dropdownlist

时间:2018-03-12 11:57:44

标签: c# asp.net-mvc

因此,当我尝试运行系统时收到此错误消息。

  

"具有密钥'问题'的ViewData项目属于' System.String'但必须是' IEnumerable< SelectListItem>'""

我该如何解决这个问题?

控制器

private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
{
    // Create an empty list to hold result of the operation
    var selectList = new List<SelectListItem>();

    // For each string in the 'elements' variable, create a new SelectListItem object
    // that has both its Value and Text properties set to a particular value.
    // This will result in MVC rendering each item as:
    //     <option value="State Name">State Name</option>
    foreach (var element in elements)
    {
        selectList.Add(new SelectListItem
        {
            Value = element,
            Text = element
        });
    }

    return selectList;
}
private IEnumerable<string> GetAllQuestions()
{
    return new List<string>
    {
        "In which city were you born?",
        ...
    };
}

模型

[Required]
[Display(Name = "Question:")]
public string question { get; set; }

public IEnumerable<SelectListItem> questions { get; set; }

查看

<div class="form-group">
    @Html.LabelFor(m => Model.question)
    @Html.DropDownListFor(m => Model.question, Model.questions, "- Please select a security question -", new { @class = "form-control" })
</div>

0 个答案:

没有答案
相关问题