[英]Convert SelectListItem to IEnumerable List
I am trying to pass a SelectListItem
to a view. 我试图将SelectListItem
传递给视图。 This checkbok list will be viewable within the view in a JavaScript dialog box with date ranges. 此复选框列表可在带有日期范围的JavaScript对话框的视图中查看。
From the following code, the method ReadLogFile
returns a correct SelectListItem
. 从以下代码中,方法ReadLogFile
返回正确的SelectListItem
。 I call this method from my controller action method. 我从控制器操作方法中调用此方法。 My entity contains a property called RunDatesDisplay
which is an IEnumerable
List. 我的实体包含一个名为RunDatesDisplay
的属性,它是一个IEnumerable
列表。 I could have created this property as a select list item but I do not want to reference the System.Web.Mvc
namespace in my domain project? 我可以创建此属性作为选择列表项,但是我不想在域项目中引用System.Web.Mvc
命名空间?
The problem is I am unable to cast the selectlist item to an IEnumerable
list in my entity to pass to the view. 问题是我无法将选择列表项强制转换为实体中的IEnumerable
列表以传递到视图。 I have created a listview model and within the view I will call the editor template to render the checkboxes. 我已经创建了一个listview模型,并且在视图中,我将调用编辑器模板来呈现复选框。
I just need help to correctly pass the selectlist item to the view. 我只需要帮助才能将selectlist项目正确传递到视图。 Should I pass the SelectListItem
to the model property using ViewBag
, for instance ViewBag.RunDatesDisplay = items(selectlistitem)
or I should bind my selectlistitem to selectlist first? 我应该使用ViewBag
将SelectListItem
传递给模型属性,例如ViewBag.RunDatesDisplay = items(selectlistitem)
还是应该将selectlistitem首先绑定到selectlist?
Entity: 实体:
public class RunLogEntry
{
public int ID { get; set; }
public int RunID { get; set; }
[NotMapped]
public bool? LoadFileAttached { get; set; }
[NotMapped]
public bool? OutFileAttached { get; set; }
[NotMapped]
public string AttachedLoadListFileName { get; set; }
[NotMapped]
public string AttachedOutputFileName { get; set; }
[NotMapped]
public List<RunLogEntryTestExceptionDisplay> TestExceptionDisplay { get; set; }
[NotMapped]
public IEnumerable<RunLogEntryDatesDisplay> RunDatesDisplay { get; set; }
}
Method which does some processing and return a selectlistitem: 进行一些处理并返回selectlistitem的方法:
public static List<SelectListItem> ReadLogFile(List<string> fileNames)
{
//Get collection of files and send to parser file by file:
LogFile lf = new LogFile();
var items = new List<SelectListItem>();
foreach (var minDate in minDates)
{
var item = new SelectListItem() { Value = minDate.ToString(), Text = String.Format("{0} - {1}", minDate, minDate.AddMinutes(30)) };
items.Add(item);
}
return items;
}
Controller Action(I did not include the entire code for the action) 控制器动作(我没有包含动作的全部代码)
//3. Send log file name collection to parser
if (logFiles != null && logFiles.Count > 0)
{
var items = new List<SelectListItem>();
items = FileImport.ReadLogFile(logFiles);
**RunLogEntry.RunDatesDisplay = FileImport.ReadLogFile(logFiles);**
ViewModel: ViewModel:
public class RunLogRunDatesViewModel
{
public IEnumerable<SelectListItem> RunLogRunDates { get; set; }
}
Editor Template 编辑器范本
@model RunLog.Domain.Entities.RunDatesDisplay
<tr>
<td>
@Html.DisplayFor(x => x.RunDate)
</td>
</tr>
And finally my view: 最后我的看法是:
<div id="runDatestreeview" title="Dialog Title" style="font-size: 10px; font-weight: normal;
overflow: scroll; width: 800px; height: 450px; display: none;">
<table class="grid" style="width: 600px; margin: 3px 3px 3px 3px;">
<thead>
<tr>
<th>
Run Dates:
</th>
</tr>
</thead>
<tbody>
@if (Model.RunDatesDisplay != null)
{
@Html.EditorFor(x => x.RunDatesDisplay)
}
</tbody>
</table>
</div>
You can create custom type with two properties, or just use Tuple
class, so your ReadLogFile
could be something like this 您可以创建具有两个属性的自定义类型,也可以只使用Tuple
类,因此您的ReadLogFile
可能是这样的
public static List<Tuple<string,string>> ReadLogFile(List<string> fileNames)
{
...
var items = new List<Tuple<string,string>>();
foreach (var minDate in minDates)
{
var item = new Tuple<string, string>(minDate.ToString(), String.Format("{0} - {1}", minDate, minDate.AddMinutes(30));
items.Add(item);
}
return items;
}
Your model: 您的模型:
public class RunLogRunDatesViewModel
{
public SelectList RunLogRunDates { get; set; }
}
And your controller: 和您的控制器:
var model = new RunLogRunDatesViewModel();
List<Tuple<string,string>> items = FileImport.ReadLogFile(logFiles);
model.RunLogRunDates = new SelectList(items, "Item1", "Item2");
SelectList will try to get Value from "Item1" and Text from "Item2" SelectList将尝试从“ Item1”获取值,从“ Item2”获取文本
Or, as I said at start you can create simple class with two properties to avoid using of Tuple 或者,就像我一开始所说的那样,您可以创建具有两个属性的简单类,以避免使用Tuple
UPDATE1 In .Net 2.0-3.5, you can create simple wrapper for it: UPDATE1在.Net 2.0-3.5中 ,您可以为其创建简单的包装器:
public class Tuple<T1,T2>
{
public T1 Item1 {get;set;}
public T2 Item2 {get;set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.