DropDownList中的SelectList的SelectedValue问题 [英] DropDownList SelectList SelectedValue issue

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

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist-to-work\">How我能得到这个ASP.NET MVC的SelectList工作?


这到底是什么呢?是否有某种在MVC3的DropDownList中的一个bug?
作为标记实际选择的SelectedValue不出来。

我尝试不同的方法,没有什么工作。

 公共类SessionCategory
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
}公共静态的IEnumerable&LT; SessionCategory&GT;分类
{
     得到
      {
          VAR _dal =新数据层();
          返回_dal.GetSesionCategories();
      }
}@ {
        VAR的猫= Infrastructure.ViewModels.Session.Categories;
        VAR SL =新的SelectList(猫,ID,姓名,2);
}
@ Html.DropDownList(类,SL);


解决方案

我认为你需要做选定值的字符串。还有使用扩展方法有一定价值详见<一个href=\"http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist-to-work\">here.

Possible Duplicate:
How can I get this ASP.NET MVC SelectList to work?

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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆