【发布时间】:2012-12-11 22:06:31
【问题描述】:
我正在尝试将 SelectListItem 传递给视图。可以在带有日期范围的 JavaScript 对话框的视图中查看此复选框列表。
从下面的代码中,ReadLogFile 方法返回一个正确的SelectListItem。我从我的控制器操作方法中调用此方法。我的实体包含一个名为RunDatesDisplay 的属性,它是一个IEnumerable 列表。我可以将此属性创建为选择列表项,但我不想在我的域项目中引用 System.Web.Mvc 命名空间?
问题是我无法将选择列表项转换为我的实体中的IEnumerable 列表以传递给视图。我创建了一个列表视图模型,并在视图中调用编辑器模板来呈现复选框。
我只需要帮助将选择列表项正确传递给视图。
我应该使用ViewBag 将SelectListItem 传递给模型属性,例如ViewBag.RunDatesDisplay = items(selectlistitem),还是应该先将我的选择列表项绑定到选择列表?
实体:
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; }
}
进行一些处理并返回一个选择列表项的方法:
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;
}
控制器动作(我没有包含动作的全部代码)
//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);**
视图模型:
public class RunLogRunDatesViewModel
{
public IEnumerable<SelectListItem> RunLogRunDates { get; set; }
}
编辑器模板
@model RunLog.Domain.Entities.RunDatesDisplay
<tr>
<td>
@Html.DisplayFor(x => x.RunDate)
</td>
</tr>
最后是我的观点:
<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>
【问题讨论】:
标签: c# asp.net-mvc