I am trying to pass a SelectListItem to a view. This checkbok list will be viewable within the view in a JavaScript dialog box with date ranges.
From the following code, the method ReadLogFile returns a correct SelectListItem. I call this method from my controller action method. My entity contains a property called RunDatesDisplay which is an IEnumerable List. 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?
The problem is I am unable to cast the selectlist item to an IEnumerable list in my entity to pass to the view. I have created a listview model and within the view I will call the editor template to render the checkboxes.
I just need help to correctly pass the selectlist item to the view.
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?
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:
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:
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
Tupleclass, so yourReadLogFilecould be something like thisYour model:
And your controller:
SelectList will try to get Value from “Item1” and Text from “Item2”
Or, as I said at start you can create simple class with two properties to avoid using of Tuple
UPDATE1
In .Net 2.0-3.5, you can create simple wrapper for it: