ropDownList中的SelectList的Selected

ropDownList中的SelectList的Selected

本文介绍了DropDownList中的SelectList的SelectedValue问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

What the hell is it? Is there some kind of a bug in DropDownList of MVC3?SelectedValue doesn't show up as actually selected in the markup.

I am trying different approaches, nothing works.

public class SessionCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public static IEnumerable<SessionCategory> Categories
{
     get
      {
          var _dal = new DataLayer();
          return _dal.GetSesionCategories();
      }
}

@{
        var cats = Infrastructure.ViewModels.Session.Categories;
        var sl = new SelectList(cats, "Id", "Name",2);
}
@Html.DropDownList("categories", sl);
解决方案

I think you need to make the selected value a string. There's also some value in using extension methods as detailed here.

这篇关于DropDownList中的SelectList的SelectedValue问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:31