我已经看过其他帖子的答案,但我不能让他们中的任何一个为我工作..所以。这是在我的控制器 - >
ViewData["TARGET_DATE"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "TARGET_DATE", "TARGET_DATE");
我希望能够在从db返回的日期应用格式,我的DDL看起来像这样
<td><%=Html.DropDownList("TARGET_DATE", "All")%></td>
有没有人知道是否有办法循环并格式化每个日期,或者将格式应用于所有日期。或者什么是最好的方法,如果你需要更多的代码我可以提供,我真正想要的是显示日期没有时间。提前谢谢。
答案 0 :(得分:2)
我知道这篇文章很老了,但我在搜索同一问题的答案时遇到了它。我最终编写了一个FormattableSelectList类,它将格式字符串作为参数,然后格式化选择列表的输出。以下是如何使用它的示例。
@Html.DropDownFor(
o => o.SomeProperty,
new FormattableSelectList(ViewBag.MyItems, "ID", "Time", "{0:f}"),
"Choose a Time"
)
我在http://www.jpolete.me/2011/11/16/a-formattable-selectlist-for-net-mvc/写了一篇关于它的简短帖子。这是代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Web.UI;
using System.Globalization;
public class FormattableSelectList : SelectList
{
private string _FormatString;
public FormattableSelectList(IEnumerable items, string dataValueField, string dataTextField, string formatString)
: base(items, dataValueField, dataTextField)
{
_FormatString = formatString;
}
public FormattableSelectList(IEnumerable items, string dataValueField, string dataTextField, string formatString, object selectedValue)
: base(items, dataValueField, dataTextField, selectedValue)
{
_FormatString = formatString;
}
public override IEnumerator<SelectListItem> GetEnumerator()
{
return ((!String.IsNullOrEmpty(DataValueField)) ?
GetListItemsWithValueField() :
GetListItemsWithoutValueField()).GetEnumerator();
}
private IList<SelectListItem> GetListItemsWithValueField()
{
HashSet<string> selectedValues = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (SelectedValues != null)
{
selectedValues.UnionWith(from object value in SelectedValues select Convert.ToString(value, CultureInfo.CurrentCulture));
}
var listItems = from object item in Items
let value = Eval(item, DataValueField, null)
select new SelectListItem
{
Value = value,
Text = Eval(item, DataTextField, _FormatString),
Selected = selectedValues.Contains(value)
};
return listItems.ToList();
}
private IList<SelectListItem> GetListItemsWithoutValueField()
{
HashSet<object> selectedValues = new HashSet<object>();
if (SelectedValues != null)
{
selectedValues.UnionWith(SelectedValues.Cast<object>());
}
var listItems = from object item in Items
select new SelectListItem
{
Text = Eval(item, DataTextField, _FormatString),
Selected = selectedValues.Contains(item)
};
return listItems.ToList();
}
private static string Eval(object container, string expression, string formatString)
{
object value = container;
if (!String.IsNullOrEmpty(expression))
{
value = DataBinder.Eval(container, expression);
}
string stringValue;
if (formatString == null)
{
stringValue = Convert.ToString(value, CultureInfo.CurrentCulture);
}
else
{
stringValue = String.Format(formatString, value);
}
return stringValue;
}
}
答案 1 :(得分:0)
以下是我实现上述课程的方法。我的客户正在寻找包含开始时间和结束时间的两个下拉列表。在MVC4中
型号:
[Display(Name = "Time (From):")]
public int FromTimeId { get; set; }
[Display(Name = "Time (To):")]
public int ToTimeId { get; set; }
public virtual FromTime FromTime { get; set; }
public virtual ToTime ToTime { get; set; }
控制器:
public ActionResult Create()
{
ViewBag.FromTimes = db.FromTimes;
ViewBag.ToTimes = db.ToTimes;
}
查看:
<div class="editor-field">
@Html.DropDownListFor(model => model.FromTimeId, new Namespace.FormattableSelectList(ViewBag.FromTimes, "FromTimeId", "FromTimeName", "{0:h:mm tt}"))<br />
@Html.ValidationMessageFor(model => model.FromTimeId)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.ToTimeId, "To Time:")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ToTimeId, new Namespace.FormattableSelectList(ViewBag.ToTimes, "ToTimeId", "ToTimeName", "{0:h:mm tt}"))<br />
@Html.ValidationMessageFor(model => model.ToTimeId)
</div>