【问题标题】:DropDownListFor display box is blank after selectionDropDownListFor 选择后显示框为空
【发布时间】:2016-12-13 12:18:44
【问题描述】:

问题:

您好,我的 DropDownListFor 突然出现问题,它工作得很好。我正在对我的网络应用程序进行一些样式设置,但在 dropdownlistfor 上没有更改任何内容

 <!-- Genre -->
    <div class="form-group">
        @Html.LabelFor(m => m.Genre)
        @Html.DropDownListFor(m => m.Genre, new SelectList(Model.Genres.OrderBy(n=>n.Name), "Id", "Name"), "Select Genre", new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Genre)
    </div>

在控制器中

 [Authorize]
    public ActionResult Create()
    {
        var viewModel = new GigFormViewModel
        {
            Genres = _context.Genres.ToList()
        };

        return View(viewModel);
    }

好的,这是我的问题的视觉效果:

Blank display box

Yet the item is selected and will save properly to database

所以困扰我的只是视觉上的事情..保存,其他一切都很好。

我什至检查了项目的元素和颜色是黑色的尝试更改显示框的背景以防字体为白色所以我可以看到它但是不,选择根本不显示。

编辑:视图模型->

 public class GigFormViewModel
{

    [Required]
    public string Name { get; set; }

    [Required]
    public string Venue { get; set; }

    [Required]
    [FutureDate]
    public string Date { get; set; }

    [Required]
    [ValidTime]
    public string Time { get; set; }

    [Required]
    public int Genre { get; set; }

    public IEnumerable<Genre> Genres { get; set; }

    public DateTime GetDateTime()
    {
        return DateTime.Parse(string.Format("{0} {1}", Date, Time));
    }

还有流派类

 public class Genre
{
    public byte Id { get; set; }


    [Required]
    [StringLength(255)]
    public string Name { get; set; }

}

【问题讨论】:

    标签: html model-view-controller dropdownlistfor


    【解决方案1】:

    试试这个改动

    @Html.DropDownListFor(m => m.Genre, new SelectList(Model.Genres.OrderBy(n=>n.Name), "Id", "Name",0), "Select Genre", new { @class = "form-control" })
    

    也许它会为你工作..;)

    【讨论】:

    • 不,不幸的是它没有。我已经尝试过了,并且使用了不同的值,例如 1 和 2..但问题仍然存在。
    • 哦..然后出了点问题..您尝试过使用文本而不是值..请尝试使用“选择流派”而不是 0。我不确定..bt 你尝试过吗像这样
    • 不行,我敢肯定。编辑:无论如何都试过了,但没有,还编辑了我的帖子以包含我的视图模型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多