Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectListItem即使显式设置为true也未选中_C#_Asp.net Mvc_Selectlistitem - Fatal编程技术网

C# SelectListItem即使显式设置为true也未选中

C# SelectListItem即使显式设置为true也未选中,c#,asp.net-mvc,selectlistitem,C#,Asp.net Mvc,Selectlistitem,这个问题与ASP.NET MVC 2 RC(12月下降)有关。基本问题是,当创建带有默认选定值的下拉列表时,“selected”属性要么没有呈现为HTML,要么归因于错误的选项 下面是一些代码来演示错误的选项问题: 下面是一个生成一些值的函数[AttributeService class]: public static Dictionary<int, string> MonthOfBirth { get { Dictio

这个问题与ASP.NET MVC 2 RC(12月下降)有关。基本问题是,当创建带有默认选定值的下拉列表时,“selected”属性要么没有呈现为HTML,要么归因于错误的选项

下面是一些代码来演示错误的选项问题:

下面是一个生成一些值的函数[AttributeService class]:

public static Dictionary<int, string> MonthOfBirth
    {
        get
        {
            Dictionary<int, string> months = new Dictionary<int, string>();
            for (int i = 0; i <= 11; i++)
            {
                months.Add(i, CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);
            }
            return months;
        }
    }
公共静态字典
{
得到
{
字典月份=新字典();

对于(int i=0;i我看不到您的控制器方法,但我相信问题就在那里。如果您在控制器中设置了以下内容,您应该会看到选中的月份

public ActionResult Edit(string id)
{
  User myModel = UserServiceFactory.GetUser(id);
  myModel.MonthOfBirth = -1;
  return View(myModel);
}

HTML看起来很好。没有太多帮助,我知道,对不起。啊哈-我想我现在明白了,它希望模型提供默认值-而我假设将适当的selectlistitem设置为selected将设置selected标志。非常感谢!
 public List<SelectListItem> MonthOfBirthList
    {
        get
        {
            return AttributeService.MonthOfBirth.OrderBy(x => x.Key).ToSelectList(x => x.Value, x => x.Key,
                                                 "Month");
        }
    }
<%=Html.DropDownListFor(x=>x.MonthOfBirth, Model.MonthOfBirthList, new { @class = "panel_4_month" })%>
<select class="panel_4_month" id="MonthOfBirth" name="MonthOfBirth">
  <option value="-1">Month</option>
  <option selected="selected" value="0">January</option>
  <option value="1">February</option>
  <option value="2">March</option>
  etc...
</select>
public ActionResult Edit(string id)
{
  User myModel = UserServiceFactory.GetUser(id);
  myModel.MonthOfBirth = -1;
  return View(myModel);
}