1

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?

Where i got my controller code example from

Pedro Lopes
  • 479
  • 2
  • 9
  • 20

3 Answers3

1

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>
Lab Lab
  • 781
  • 10
  • 34
0

The main problem is that you are trying to iterate through your Model which isn't iterable. Second thing that bothers me is that you have DisplayFor referencing to nothing (next to the month label).

I don't know exactly what you are trying to achieve but your code has couple of errors, but try changing the following (I omitted some code for the brevity):

<dl class="dl-horizontal">  
    <dt>
      Year
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Info.Year)
    </dd>   
</dl>


 foreach(var detail in model.Details)
 {
        <tr>
            <td>
                @Html.DisplayFor(x=> detail.DayOfMonth)
            </td>
            <td>
                @Html.DisplayFor(x=> detail.Project)
            </td>
        </tr>
 } 
wegelagerer
  • 3,600
  • 11
  • 40
  • 60
0

Your model is a single object that has within it an enumeration, so this needs to change:

@model IEnumerable<TimeTrackerProjectV2.Models.TimesheetInfoAndTimeSheet_DetailsViewModel>

to

@model TimeTrackerProjectV2.Models.TimesheetInfoAndTimeSheet_DetailsViewModel

You don't need a foreach loop to reference you model.Info as there is only one so the first foreach loop can go and you can simply change references from

@Html.DisplayFor(model => item.Info.Year)

to

@Html.DisplayFor(model => model.Info.Year)

Your final foreach loop will change from

@foreach (var item in Model)

to

 @foreach (var item in Model.Details)
Kell
  • 3,252
  • 20
  • 19