When using viewmodel can not convert list to entities

Hi I use viewmodel and in my controller can not convert list to entity.

Here is my viewmodel:

namespace SmartAdvertising.Backend.Models
{
    public class DashboardModel
    {
        public Advert allAdvert { get; set; }
        public Advert allAdvert2 { get; set; }
    }
}

Controller:

   public ActionResult Dashboard()
         {
             string email_uzivatele = (String)Session["email"];
             DashboardModel adverts = new DashboardModel();

             List<Advert> time = AdvertServiceLayer.Instance.SelectAllWith(email_uzivatele, "Reklama na určitou dobu");
             List<Advert> price = AdvertServiceLayer.Instance.SelectAllWith(email_uzivatele, "Reklama na určitou částku");
             adverts.allAdvert = time;
             adverts.allAdvert2 = price;
             return View(adverts);
         }

View:

@model IList<SmartAdvertising.Backend.Models.DashboardModel>

@{
    ViewBag.Title = "Dashboard";
}

<h1>@ViewBag.Title</h1>

<div class="row">
    <div class="col-md-12 col-sm-12">
        <h2>Všechny reklamy na určitou dobu</h2>


        <table class="table-hover"  style="width:100%">
            <tr>
                <th>
                    Název reklamy
                </th>
                <th>
                    Typ reklamy
                </th>
                <th>
                    Cena reklamy
                </th>
                <th>
                    Email uživatele
                </th>

                <th>
                    Datum začátku
                </th>
                <th>
                    Datum konce
                </th>
                <th>

                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @item.nazev_reklamy
                    </td>
                    <td>
                        @item.typ_reklamy
                    </td>
                    <td>
                        @item.cena_reklamy
                    </td>
                    <td>
                        @item.email_uzivatele
                    </td>
                    <td>
                        @item.datumz
                    </td>
                    <td>
                        @item.datumk
                    </td>

                </tr>
            }
        </table>

    </div>

    <div class="col-md-12 col-sm-12">
        <h2>Všechny reklamy na určitou částku</h2>


        <table class="table-hover" style="width:100%">
            <tr>
                <th>
                    Název reklamy
                </th>
                <th>
                    Typ reklamy
                </th>
                <th>
                    Cena reklamy
                </th>
                <th>
                    Email uživatele
                </th>


                <th>

                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @item.nazev_reklamy
                    </td>
                    <td>
                        @item.typ_reklamy
                    </td>
                    <td>
                        @item.cena_reklamy
                    </td>
                    <td>
                        @item.email_uzivatele
                    </td>

                </tr>
            }
        </table>

    </div>

    <div class="col-md-12 col-sm-12">

    </div>
</div>

But in view it is giving this error:

Error 1 foreach statement cannot operate on variables of type 'SmartAdvertising.Backend.Models.DashboardModel'
because 'SmartAdvertising.Backend.Models.DashboardModel' does not contain a public definition for 'GetEnumerator'

And in controller these two lines:

 adverts.allAdvert = time;
 adverts.allAdvert2 = price;

are giving me this error :

Error   3   Cannot implicitly convert type 'System.Collections.Generic.List<SmartAdvertising.Entities.Advert>'
to 'SmartAdvertising.Entities.Advert'   

Answers


Properties in model must be of type List:

public class DashboardModel
{
    public List<Advert> allAdvert { get; set; }
    public List<Advert> allAdvert2 { get; set; }
}

Additionally, you have to iterate over the properties of the model:

@foreach (var item in Model.allAdvert)

Need Your Help

JavaScript no redirect on post

javascript url post attributes

I was looking at this POST, and i was wondering, is there a way to make this function work as it should but without redirecting to a different page?

Rails image disappears with error: Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH

ruby-on-rails ruby-on-rails-4 asset-pipeline

Essentially my problem is that a page loads fine and looks normal. After about 30 seconds it suddenly disappears.