Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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# 以ShortDateString格式显示我的选择列表项_C#_Asp.net Mvc_Asp.net Mvc 4_Datetime - Fatal编程技术网

C# 以ShortDateString格式显示我的选择列表项

C# 以ShortDateString格式显示我的选择列表项,c#,asp.net-mvc,asp.net-mvc-4,datetime,C#,Asp.net Mvc,Asp.net Mvc 4,Datetime,我不确定我做错了什么。我有一个带有SelectList属性的模型,该属性将包含多个日期作为其值。我想在不添加时间戳的情况下显示这些值。如何以shortdatetime格式显示这些日期?我有以下ViewModel: public class EditWeightsViewModel { [DisplayName("Associates")] public SelectList AssociatesList { get; set; } [DisplayName("Week")

我不确定我做错了什么。我有一个带有SelectList属性的模型,该属性将包含多个日期作为其值。我想在不添加时间戳的情况下显示这些值。如何以shortdatetime格式显示这些日期?我有以下ViewModel:

public class EditWeightsViewModel
{
    [DisplayName("Associates")]
    public SelectList AssociatesList { get; set; }
    [DisplayName("Week")]
    public SelectList WeeksOfEntryList { get; set; }

    public decimal Weight { get; set; }
}
这是我控制器的一部分(*注意,weeks是日期时间列表):


我的下拉列表显示的是
System.Web.MVC.SelectListItem
,而不是实际日期。我做错了什么?我这样做错了吗?创建以ShortDateTime格式显示的编辑器模板会更容易吗?

Html.DropDownListFor
helper方法的第二个参数是
SelectListItem
的集合。因此,将
WeeksOfEntryList
属性的类型更改为
SelectListItem
的列表。我还添加了另一个属性,
SelectedWeek
来存储所选的选项值

public class EditWeightsViewModel
{
    public string SelectedWeek {set;get;}

    [DisplayName("Week")]
    public List<SelectListItem> WeeksOfEntryList { get; set; }          

    [DisplayName("Associates")]
    public SelectList AssociatesList { get; set; }

    public decimal Weight { get; set; }
}
在你看来

@using YourNameSpaceHere.EditWeightsViewModel

@Html.DropDownListFor(s=>s.SelectedWeek, Model.WeeksOfEntryList ,"Select")

怎么了?日期是否未以短日期格式显示?它们是如何显示的?您忽略了告诉我们您遇到的实际问题。你的代码看起来不错。我说我的下拉列表显示的是
System.Web.MVC.SelectListItem
,而不是实际日期。对不起,我想这有点含糊不清。这似乎奏效了。知道为什么我的版本不起作用吗?@Thevanilla,因为你从第一个(内部选择列表)创建了一个(无意义的)第二个选择列表,但没有设置值和文本属性。
public ActionResult Create()
{
  var vm = new EditWeightsViewModel();
  vm.WeeksOfEntryList = weeks.Select(s=> new SelectListItem
                       { Value=s.ToShortDateString(),
                         Text=s.ToShortDateString()}).ToList();
  //If you want to keep one option selected, Set the vm.SelectedWeek property value.


  return View(vm);
}
@using YourNameSpaceHere.EditWeightsViewModel

@Html.DropDownListFor(s=>s.SelectedWeek, Model.WeeksOfEntryList ,"Select")