Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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# MVC selectlist不会在下拉列表中显示selecteditem_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# MVC selectlist不会在下拉列表中显示selecteditem

C# MVC selectlist不会在下拉列表中显示selecteditem,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我在Repository.cs中有以下代码来获取时区 public static IEnumerable<SelectListItem> GetTimeZoneList() { return TimeZoneInfo.GetSystemTimeZones().Select(x => new SelectListItem() { Value = x.Id, Text = x.DisplayName }); } 在

我在
Repository.cs
中有以下代码来获取
时区

public static IEnumerable<SelectListItem> GetTimeZoneList()
{
     return TimeZoneInfo.GetSystemTimeZones().Select(x => new SelectListItem()
     {
         Value = x.Id,
         Text = x.DisplayName
     });
}
在ajax调用中,我按如下方式填写模型值:

public class AddEditEmployeeViewModel
{
   public string TimeZoneID { get; set; }
   public SelectList TimeZones { get; set; }
}
public PartialViewResult GetAddEditEmployee(string id)
{
    var model = new AddEditEmployeeViewModel();
    model.TimeZones = new SelectList(Repository.GetTimeZoneList(), "Value", "Text");
    var employee = (from e in context.tbl_users where e.eid == eid select new { e.fnamae, e.lname, e.account_status, e.preferred_timezone }).FirstOrDefault();
    if (employee == null) return PartialView("_AddEditEmployee", model);
    model.TimeZoneID = employee.preferred_timezone;
    //model.TimeZoneID will have values like India Standard Time, UTC etc.,
    //..Other properties
    return PartialView("_AddEditEmp", model);
}
即使
model.TimeZoneID
从selectlist中获取了匹配的值,它也不会将值项保持为选中状态。我可以在
SelectList
中看到从DB获取的值。截图供参考

这是查看代码的

@Html.DropDownListFor(m => m.TimeZoneID, Model.TimeZones, "Select a Timezone", new { @class = "selectpicker", data_width = "100%", })

我在这里犯了什么错误。需要做哪些更改才能从
dropdownlist/selectpicker
中选择项目?

通常,我会替换

model.TimeZones = new SelectList(
    Repository.GetTimeZoneList(), 
    "Value", 
    "Text");

基本上,使用。您也可以简单地返回时区对象,而不是SelectListItems的集合:

public static IEnumerable<TimeZone> GetTimeZoneList()
{
     return TimeZoneInfo.GetSystemTimeZones();
}

@GuruprasadRao消除显而易见的问题:你重建了吗?@GuruprasadRao你的问题中没有任何东西可以说明为什么你的价值不会被选中。我唯一的另一个想法是,当涉及到相等比较时,字符串是奇怪的野兽,所以也许您可以尝试向模型和时区对象添加一个整数字段,指定一个任意值,然后查看绑定是否正确。我知道我提到的过载是有效的,因为这是我通常使用的。。好的@Tieson。。可能是我的代码中的某些东西也会为此而阻塞。。我必须找到答案。。无论如何。。。我会把你的步骤当作积木,并在明天之前接受答案。。谢谢你抽出时间……)如果要在数据库表中存储时区,并且不想使用自动递增ID,则始终可以使用GUID(SQL Server中的“uniqueidentifier类型”)。您可能还希望在字符串上应用Trim()函数,因为额外的空格将抛出类似于相等字符串比较的内容。当使用
DropDownListFor()
绑定到属性时,在
SelectList
构造函数中添加第四个参数是没有意义的。它被忽略,因为在内部,该方法构建了一个新的
IEnumerable
,并根据属性的值设置
Selected
属性。在返回部分视图之前,您在
model.TimeZoneID
中是否有有效的值?是的,我有,如上面代码的注释所述@Shyju您的代码看起来非常好。下面是我从您的代码中创建的一个工作示例,它与您的代码无关,但是
GetTimeZoneList()
返回
IEnumerable
,这就是
SelectList
的含义,因此没有必要从第一个创建相同的第二个
SelectList
。属性可以是
public IEnumerable TimeZones{get;set;}
,然后只需
model.TimeZones=Repository.GetTimeZoneList()。正如其他人所指出的,如果
TimeZoneID
的值恰好与集合中的一个值匹配,则代码应该可以正常工作
public static IEnumerable<TimeZone> GetTimeZoneList()
{
     return TimeZoneInfo.GetSystemTimeZones();
}
model.TimeZones = new SelectList(
    Repository.GetTimeZoneList(), 
    "ID", 
    "DisplayName", 
    model.TimeZoneID);