I would like to using two times @model to get data from another part of my website is it possible? Because now I have got error but if I have only this first @model everything working correct.
Look -> MVC 3 - Exception Details: System.InvalidOperationException
Error 2 'SportsStore.Entities.Kategorie' does not contain a definition for 'Opis' and no extension method 'Opis' accepting a first argument of type 'SportsStore.Entities.Kategorie' could be found (are you missing a using directive or an assembly reference?) c:\Users\Rafal\Desktop\MVC ksiązka\moj projekt\sklep\SportsStore.WebUI\Views\Product\List.cshtml 16 4 SportsStore.WebUI
@model IEnumerable<SportsStore.Entities.Towar>
@model IEnumerable<SportsStore.Entities.Kategorie>
@{
ViewBag.Title = "List";
}
<h2>List</h2>
@foreach (var p in Model)
{
<div class="item">
<h3>@p.Nazwa</h3>
@p.Opis
<h4>@p.Cena.ToString("c")</h4>
</div>
}
I think this would be a case to create a ViewModel (to combine the two entities you have) and then base a View off that ViewModel.
It is best to create a view model to represent your data. Your view model must only contain what you need to display on your view. Anything that is not used you can remove, no point of it being there. Your view model can look like this:
Lets say that you have to use this view model in a create view then your create action can look something like this:
And then in your view:
You only can have one Model per View. But you can use another object to declarate the model:
And then use it in your view like this:
I would also recommend you to use english names in MVC. It's more clear to read and understand ;).