【问题标题】:how to format the datetime using Html.DropDownListFor(...)如何使用 Html.DropDownListFor(...) 格式化日期时间
【发布时间】:2015-06-08 22:19:39
【问题描述】:

我想要一个短日期格式的下拉列表。 但以下都不起作用。 我有

[Property]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? DepartrueDate { get; set; }

在视图中,我尝试了

<div>@Html.DropDownListFor(m => m.TripDepartures.First().DepartrueDate, new SelectList(Model.TripDepartures.Select(s=>
         new SelectListItem { Value = s.Id.ToString(), Text = s.DepartrueDate.Value.ToShortDateString() }).ToList()))</div>
</div>

它没有输出; 我也试过了

 <div>@Html.DropDownListFor(m => m.TripDepartures.First().DepartrueDate, new SelectList(Model.TripDepartures, "Id","DepartrueDate"))</div>
                </div>

它给了
我怎么能得到一个短暂的约会?

【问题讨论】:

    标签: asp.net-mvc html.dropdownlistfor


    【解决方案1】:

    理想情况下,您不希望将域实体 (TripDepartures) 从 Controller 传递给 View。这不是一个好的设计实践。

    相反,您想传递 SelectListItem

    查看

    @using (Html.BeginForm())
    {
        @Html.DropDownListFor(m => m.SelectedTripDepartureId, Model.AvailableTripDepartures)
        <input type="submit" value="Submit" />
    }
    

    型号

    public class HomeModel
    {
        public int SelectedTripDepartureId { get; set; }
        public IList<SelectListItem> AvailableTripDepartures { get; set; }
    
        public HomeModel()
        {
            AvailableTripDepartures = new List<SelectListItem>();
        }
    }
    

    控制器

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HomeModel model = new HomeModel();
            model.AvailableTripDepartures = GetTripDepartures()
                .Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text = s.DepartrueDate.Value.ToString("MM/dd/yyyy")
                }).ToList();
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(HomeModel model)
        {
            int tripDepartureId = model.SelectedTripDepartureId;
    
            DateTime? departrueDate = GetTripDepartures()
                .Where(x => x.Id == tripDepartureId)
                .Select(x => x.DepartrueDate)
                .FirstOrDefault();
    
            return View(model);
        }
    
        // This data will be from persistent storage such as database.
        private IList<TripDeparture> GetTripDepartures()
        {
            return new List<TripDeparture>
            {
                new TripDeparture {Id = 1, DepartrueDate = DateTime.Now.AddDays(1)},
                new TripDeparture {Id = 2, DepartrueDate = DateTime.Now.AddDays(2)},
                new TripDeparture {Id = 3, DepartrueDate = DateTime.Now.AddDays(3)},
            };
        }
    }
    

    TripDeparture 班

    public class TripDeparture
    {
        public int Id { get; set; }
        public DateTime? DepartrueDate { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      不应该是:

      @Html.DropDownListFor(m => m.TripDepartures.First().Id.ToString(), new SelectList(Model.TripDepartures.Select(s=>
           new SelectListItem() { Value = s.Id.ToString(), Text = s.DepartrueDate.Value.ToShortDateString() }).ToList()))
      

      如果它不起作用: 你确定 Model.TripDepartures 有元素吗?

      我记得我自己在模型中添加了几次 SelectList,所以你可以尝试在你的模型中添加一个属性,即 SelectList 类型。您可以为每个 DepartureDate 添加一个 SelectListItem ,自己设置 id 和 value 并自己循环和调试。它与您的 Linq 代码基本相同,但我记得我这样做是有原因的。

      Model.DepartureDateSelectList = new SelectList();
      foreach (TripDeparture tripDeparture in Model.TripDepartures) {
      {
          SelectListItem item = new SelectListItem();
          item.Value = tripDeparture.Id.ToString();
          item.Text = tripDeparture.DepartueDate.Value.ToShortDateString(); //Didn't do a null check, since you didn't do it in you code either. You should change DataType to non-nullable or perform a check and handle if it's null
          Model.DepartureDateSelectList.Add(item);
      }
      

      如果您确实看到了列表,但未设置默认选定项,则将建议的更改应用于选定项表达式。如果仍然没有正确设置,您可以自己在循环中设置 selected 属性。 DropDownListFor 中有一个错误会导致所选项目并不总是被设置。

      【讨论】:

        【解决方案3】:

        您可以欺骗系统并绑定到字符串字段而不是日期字段:

        [Property]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime? DepartrueDate { get; set; }
        
        public string DepartrueDateText { get { return DepartrueDate.ToShortDataString(); } }
        

        不要忘记你的空检查。 :)

        我为枚举字段做了很多这样的操作,以使 [Display(Name="")] 属性被识别。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-12
          • 2018-05-20
          • 1970-01-01
          • 2017-08-01
          • 1970-01-01
          相关资源
          最近更新 更多