我有这个方法可以生成3个日期。我希望能够在下拉列表中使用日期。我需要它返回一个selectlistItem。我如何转换它?
public static IEnumerable<MyDates> GenerateLastThreeDates()
{
IEnumerable<MyDates> rangeList = new[] { 1, 2, 3, 4 }.Select(n => DateTime.Now.Subtract(TimeSpan.FromDays(n))) // Transforming the integer (kind of index of days) array into dates.
.Where(n => n.DayOfWeek != DayOfWeek.Sunday).Take(3) // Removing the Sunday and taking 3 days only.
.Select(n => new MyDates() { Dateseries = n }); // Converting the date in your MyDates structure.
return rangeList.ToList();
}我试过了,但是没有成功。
IEnumerable<SelectListItem> myCollection = GenerateLastThreeDates
.Select(i => new SelectListItem()
{
Text = i.ToString(),
Value = i
});
public class MyDates
{
public DateTime Dateseries { get; set; }
}发布于 2016-11-12 06:25:05
将controller
SelectListItem ViewData中的列表中-这就是如何使用ViewData:将非模型(即单向数据,而不是双向数据)传递到列表中的视图
例如:
控制器
public class FooController : Controller
{
private static IEnumerable<SelectListItem> GenerateLastThreeDates()
{
// note that your code should be timezone-aware, so consider using DateTimeOffset instead of DateTime
// also, never reference DateTime.Today (or Now, or UtcNow) in a loop - because the value can change in successive calls, instead you should store the value and re-use the cached copy
DateTime now = DateTime.Today;
return Enumerable.Range( 1, 4 )
.Select( n => now.AddDays( -n ) )
.Where( d => d.DayOfWeek != DayOfWeek.Sunday )
.Take( 3 )
.Select( d => new SelectListItem()
{
Value = d.ToString("o"), // "o" for the RoundTrip format
Text = d.ToString("d")
} );
}
[HttpGet]
public ActionResult Index()
{
this.ViewData["dates"] = GenerateLastThreeDates();
return this.View( new IndexViewModel() );
}
[HttpPost]
public ActionResult Index(IndexViewModel viewModel)
{
if( this.ModelState.IsValid )
{
DoSomething( viewModel.SelectedDate );
return this.RedirectToAction( nameof(this.Index) );
}
else
{
this.ViewData["dates"] = GenerateLastThreeDates();
return this.View( viewModel );
}
}}
ViewModel.cs
public class IndexViewModel {
[Required]
public DateTime SelectedDate { get; set; }
}View.cshtml
@{ IEnumerable<SelectListItem> dates = (IEnumerable<SelectListItem>)this.ViewData["dates"]; }
<p>Select a date: @Html.DropDownListFor( m = m.SelectedDate, dates )</p>发布于 2016-11-12 07:07:56
您的GenerateLastThreeDates方法返回一个MyDates集合,该集合中的每个项都有一个Dateseries属性,您将为该属性设置日期和时间。因此,在执行select方法调用时,基本上需要选择该属性。
var myCollection = GenerateLastThreeDates()
.Select(i => new SelectListItem
{
Text = i.Dateseries.ToString(),
Value = i.Dateseries.ToString()
});myCollection是SelectListItem的集合,其中的Text和value属性保存日期值的测试版本。您可以使用此类通过Html.DropDownListFor或Html.DropDownList辅助方法来构建select元素。
发布于 2016-11-12 08:11:19
我通常只像这样定义扩展方法:
public static List<SelectListItem> ToSelectListItems<T>(this IEnumerable<T> collection, Func<T, object> textSelector, Func<T, object> valueSelector, string emptyText = "- Choose-",
string emptyValue = null)
{
var result = new List<SelectListItem>();
if (collection != null)
{
var items = collection
.Select(x => new SelectListItem
{
Text = textSelector(x)?.ToString(),
Value = valueSelector(x)?.ToString()
})
.ToList();
result.AddRange(items);
}
if (emptyText != null)
{
result.Insert(0, new SelectListItem { Text = emptyText, Value = emptyValue ?? string.Empty });
}
return result;
}然后:
1)将可能的值作为任意IEnumerable<T>添加到模型中
public class MyModel
{
public int CountryId { get; set; }
public List<Country> AllCountries { get; set; } = new List<Country>();
}2)在控制器操作中填充可能的值:
public ActionResult Index()
{
var model = new MyModel
{
AllCountries = _repository.GetCountries();
};
return View(model);
}3)直接在视图中使用该扩展方法:
@Html.DropDownListFor(x => x.CountryId, Model.AllCountries.ToSelectListItems(x => x.CountryName, x => x.CountryId))https://stackoverflow.com/questions/40557066
复制相似问题