-1

I have this two classes:

public class Kolobezka : IEntity1
{
    public virtual int Id { get; set; }
    public virtual int VelikostKola { get; set; }
    public virtual string Znacka { get; set; }
    public virtual string Stav { get; set; }
    public virtual string Description { get; set; }
    public virtual Kategorie_kolobezek Kategorie { get; set; }
    public virtual int Dostupnost { get; set; }

}
public class Rezervace : IEntity1
    {
        public virtual int Id { get; set; }
        public virtual Kolobezka Rez_kolobezky { get; set; }    
        [Required(ErrorMessage = "Jméno je požadováno!")]
        public virtual string Jmeno { get; set; }   
        [Required(ErrorMessage = "Příjmení je požadováno!")]
        public virtual string Prijmeni { get; set; }  
        [Required(ErrorMessage = "Datum začátku rezervace je požadováno!")]
        public virtual DateTime Datum_od { get; set; }  
        [Required(ErrorMessage = "Datum konce rezervace je požadováno!")]
        public virtual DateTime Datum_do { get; set; }   
        public virtual string Popis { get; set; }
    }

So I need in one view called Index use two Ilists from those two clases because of this:

@foreach (Kolobezka k in Model)
        {

            foreach (Rezervace r1 in r)
            {
                if(k.Id == r1.Rez_kolobezky.Id && r1.Datum_od < DateTime.Now && r1.Datum_do > DateTime.Now)
                {
                    k.Dostupnost = 1;
                }
            }

            if (k.Dostupnost == 0)
            {
                <tr class="bg-danger">
                    <td>
                        @Ajax.ActionLink(k.Id.ToString(), "Detail", new { id = k.Id },
                       new AjaxOptions() { UpdateTargetId = "modalContent",
                           InsertionMode = InsertionMode.Replace, OnBegin = "openModalWindow" })
                    </td>
                    <td>@k.VelikostKola</td>
                    <td>@k.Znacka</td>
                    <td>@k.Kategorie.Nazev</td>
                    <td>@k.Stav</td>
                </tr>
            }

Whot should I write instead r in the second foreach. The beginning of index view looks like this:

@using ClassLibrary1.Model
@model IList<Kolobezka>

@{
    ViewBag.Title = "Index";        
}

Thank you a lot.

pajasv
  • 101
  • 2
  • 11
  • Unless I'm missing something, why do you have `@model IList` ? That only gives you access to the list of `Kolobezka` and not `Rezervace` should you not have a class specifically designed for the view containing possibly 2 properties for `IList` and `IList` and reference it similar to `@model YourNewClass` – Nope Mar 30 '17 at 12:54
  • Or access `Kolobezka` through the `Rezervace.Rez_kolobezky` property, potentially using a `GroupBy` if they should be grouped by Kolobezkas? – Georg Patscheider Mar 30 '17 at 12:57
  • I don't understand why you iterate through a list of `Kolobezka` with an **internal** loop for a list of `Rezervace` if that is not how your relationship is designed. The only relation I see is a single `Kolobezka` in the `Rezervace` class. Anyway if you are asking how to pass multiple models into a view there is plenty of SO posts showing so ► [**http://stackoverflow.com/questions/4764011/multiple-models-in-a-view**](http://stackoverflow.com/questions/4764011/multiple-models-in-a-view) – Nope Mar 30 '17 at 13:00
  • Can you post, code of your controller class, and View code after modifications?. – Yogi Mar 30 '17 at 14:38

4 Answers4

0

Create another class and create 2 properties each of the type you want.

public class Compound {
    public Kolobezka foo {get; set;}
    public Rezervace bar {get; set;}
}

Then you can pass this compound object to the view like this.

@model Compound

Now you can access the objects. You can also have list instead of single object in case you want that. Hope that this helps

It's a trap
  • 1,333
  • 17
  • 39
0

To be able to use both models, viz. Kolobezka and Rezervace, it is suggested to create a ViewModel for this, which will have properties for both these classes. Something like this for example -

public class KolobezkaRezervaceViewModel
{
    public IEnumerable<Kolobezka> Kolobezkas { get; set; }
    public IEnumerable<Rezervace> Rezervaces { get; set; }
}

You can then, bind you View with this ViewModel, that we have created just now, like -

@model MyWebApplication.Models.KolobezkaRezervaceViewModel

Now you can simply use them in your view like this -

@foreach (Kolobezka k in Model.Kolobezkas)

...and

foreach (Rezervace r1 in Model.Rezervaces)

Make sure you are correctly populating the values for you model from your controller.

Yogi
  • 9,174
  • 2
  • 46
  • 61
  • It doesnt work for me... when I add Model.Kolobezkas or Model.Rezervaces it gives me an error: "IList does not contain a definition for "Kolobezka and no extension methot Kolobezka accepting a first argument of type IList could be found (atre you missing a using directiveor an assembly references?" – pajasv Mar 30 '17 at 13:56
  • using ClassLibrary1.Model model ClassLibrary1.Model.Parentmodel this is my code at the begining of class now – pajasv Mar 30 '17 at 13:58
  • @pajasv: You do not need to bind your view to `@model IList`. Instead, bind your view with the *new* Viewmodel that we have created (with namespace, where the class resides). You may refer some tutorials to get started with how mvc works. – Yogi Mar 30 '17 at 14:21
  • @Yogi now it gives me an error saying The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ClassLibrary1.Model.Kolobezka]', but this dictionary requires a model item of type 'ClassLibrary1.Model.Parentmodel'. – pajasv Mar 30 '17 at 14:34
0

Why don't you create a class view-model and on the controller you return the object(created on view model) and build in the view what is returned on the controller.

@x.ViewModel.Name
J.Fernandes
  • 87
  • 2
  • 10
-2

I don't think you need two models. The below is not using any HTML markup why do you need to do in view. You have to do this in controller and pass Kolobezka list to view

foreach (Rezervace r1 in r)
        {
            if(k.Id == r1.Rez_kolobezky.Id && r1.Datum_od < DateTime.Now && r1.Datum_do > DateTime.Now)
            {
                k.Dostupnost = 1;
            }
        }

But if still want two models passed to view create a view model and add these classes to it as properties

public class viewmodel 
{
public Rezervace model1 { get;set;}
public Kolobezka model2 { get;set;}
}
Krishna
  • 1,945
  • 1
  • 13
  • 24