[英]How can i sort and display <IList> elements in radio button?
I have data IList<String>
type in my Model returning string date in yyyyMM
format, I need help to display array elements in radio button sorted descending order in MM/yyyy
format.我的 Model 中有数据
IList<String>
类型以yyyyMM
格式返回字符串日期,我需要帮助以单选按钮以MM/yyyy
格式降序显示数组元素。
My Model look like as follows:我的 Model 如下所示:
public class DataToLoad
{
public List<string> Months{ get; set; }
}
In my view I have the following to display array element in radio button.在我看来,我有以下在单选按钮中显示数组元素。
foreach (var record in Model.Months)
{
<div class="radio col-12">
<div class="row">
<div class="col-5" style="">
@Html.RadioButton("Months", record, true, new { id = record, @class = "m-r" })<label>@record</label>
</div>
</div>
</div>
}
I need if Months
return 199008 and 19909, I want to sort values descending and format as MM/yyyy and display the values using radio button 09/1990 first then 08/1990
.如果
Months
返回 199008 和 19909,我需要对值进行降序排序并格式化为 MM/yyyy 并09/1990 first then 08/1990
。 Any Help?有什么帮助吗?
You can use this code:您可以使用以下代码:
foreach (var record in Model.Months.Select(x=>$"{x.Substring(4,2)}/{x.Substring(0,4)}").OrderByDescending(x=>x))
{
<div class="radio col-12">
<div class="row">
<div class="col-5" style="">
@Html.RadioButton("Months", record, true, new { id = record, @class = "m-r" })<label>@record</label>
</div>
</div>
</div>
}
Same as any other piece of C# code, use Linq:与任何其他 C# 代码相同,使用 Linq:
@foreach (var record in Model.Months.OrderBy(x => x))
{
<div class="radio col-12">
<div class="row">
<div class="col-5" style="">
@Html.RadioButton("Months", record, true, new { id = record, @class = "m-r" })<label>@record</label>
</div>
</div>
</div>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.