So what im trying to do is make a view with 2 of my models. For that i made this viewmodel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TimeTrackerProjectV2.Models
{
public class TimesheetInfoAndTimeSheet_DetailsViewModel
{
public IEnumerable<TimeSheet_Details> Details { get; set; }
public TimeSheetInfo Info { get; set; }
}
}
I have a controller method that returns my viewmodel to my view like this:
public ActionResult Edit(int id)
{
var inf = Repositories.TimeSheetRepository.GetTimesheetInfo(id);
var time = Repositories.Timesheet_DetailsRepo.GetTimeSheetThroughId(id);
var model = new TimesheetInfoAndTimeSheet_DetailsViewModel { Details = time.ToList(), Info = inf };
return View(model);
}
My view currently looks like this:
@model IEnumerable<TimeTrackerProjectV2.Models.TimesheetInfoAndTimeSheet_DetailsViewModel>
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<dl class="dl-horizontal">
@foreach (var item in Model)
{
<dt>
Month
</dt>
<dd>
@Html.DisplayFor(model =>)
</dd>
<dt>
Year
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Year)
</dd>
<dt>
Year
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Year)
</dd>
<dt>
Status
</dt>
<dd>
@Html.DisplayFor(model => item.Info.StatusUnparsed)
</dd>
<dt>
User
</dt>
<dd>
@Html.DisplayFor(model => item.Info.EmailUser)
</dd>
<dt>
Explanation
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Explanation)
</dd>
}
}
</dl>
<table class="table">
<tr>
<th>
Date
</th>
<th>
Project
</th>
<th>
Task
</th>
<th>
Time Spent
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
foreach( var i in item.Details)
{
<tr>
<td>
@Html.DisplayFor(modelItem => i.DayOfMonth )
</td>
<td>
@Html.DisplayFor(modelItem => i.Project)
</td>
<td>
@Html.DisplayFor(modelItem => i.Task)
</td>
<td>
@Html.DisplayFor(modelItem => i.TimeSpent)
</td>
</tr>
}
}
</table>
I get an error because im passing a single ModelView object while my view is expecting a ennumeration. But when i change so that my view expects a single object i cant call the value of my Object Entities inside my ViewModel because i cant do a foreach with my viewmodel anymore. Hope it makes sence what im trying to say. What is the best way to accomplish this?
There's no reason to use a declaration of IEnumerable in your view. Just put those lines in your view page:
@model TimesheetInfoAndTimeSheet_DetailsViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<dl class="dl-horizontal">
@foreach (var item in Model.Details)
{
<dt>
Month
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Month)
</dd>
<dt>
Year
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Year)
</dd>
<dt>
Year
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Year)
</dd>
<dt>
Status
</dt>
<dd>
@Html.DisplayFor(model => item.Info.StatusUnparsed)
</dd>
<dt>
User
</dt>
<dd>
@Html.DisplayFor(model => item.Info.EmailUser)
</dd>
<dt>
Explanation
</dt>
<dd>
@Html.DisplayFor(model => item.Info.Explanation)
</dd>
}
</dl>
<table class="table">
<tr>
<th>
Date
</th>
<th>
Project
</th>
<th>
Task
</th>
<th>
Time Spent
</th>
<th></th>
</tr>
@foreach (var item in Model.Details)
{
<tr>
<td>
@Html.DisplayFor(modelItem => i.DayOfMonth )
</td>
<td>
@Html.DisplayFor(modelItem => i.Project)
</td>
<td>
@Html.DisplayFor(modelItem => i.Task)
</td>
<td>
@Html.DisplayFor(modelItem => i.TimeSpent)
</td>
</tr>
}
</table>