如何格式化出现在Html.DropDownListFor中的日期? [英] How to format the dates that appear in Html.DropDownListFor?

查看:67
本文介绍了如何格式化出现在Html.DropDownListFor中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象:

class Milk {
    public DateTime ExpirationDate {get;set;}
    ...
}

在模型的集合中:

class GroceryModel {
    public IList<Milk> Milks {get;set;}
    public Milk SelectedMilk {get;set;}
}

我正在使用Razor填充下拉列表:

And I'm populating a dropdown list using Razor:

@Html.DropDownListFor(m => m.ExpirationDate, Model.Milks, ...);

如何格式化该下拉列表中显示的日期?

How can I format the dates that appear in that dropdown list?

推荐答案

在您的控制器中:

model.SelectMilk将保存一个SelectListItems列表

model.SelectMilk will hold a list of SelectListItems

        model.SelectMilk =
            Milks.Select(
                item =>
                new SelectListItem
                    {
                        Selected = false, 
                        Value = item.ExpirationDate , 
                        Text = datetime.ToShortDateString() //or whatever format you need
                    }).ToList();

这篇关于如何格式化出现在Html.DropDownListFor中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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