Can you help me how to create my own ViewData.Model from mesto?
public ActionResult Index()
{
ViewData["Message"] = "Moji Priatelia";
var friends = from friend in friendsDb.FriendsTables
orderby friend.Priezvisko
select friend;
var mesto = from mesta in friendsDb.MestoTables
orderby mesta.Mesto
select mesta;
return View(friends);
}
when i add new view ViewData.Model have data from friends but no from mesto how can i create model for mesto?
Thanks a lot i am Beginner.
Here i have problem.
public class IndexViewModel
{
public IEnumerable<Friend> Friends { get; set; }
public IEnumerable<Mesta> Mesto { get; set; }
}
The type or namespace name ‘Friend’ could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name ‘Mesta’ could not be found (are you missing a using directive or an assembly reference?)
But table have names FriendTable, MestoTable if i use this names it is corect. Then i do new View via IndexViewModel but i can’t
use this
<%: Model.Friends %>
<%: Model.Mesto %>
this stops with error
The model item passed into the dictionary is of type ‘MVCApp.Controllers.IndexViewModel’, but this dictionary requires a model item of type ‘System.Collections.Generic.IEnumerable`1[MVCApp.Controllers.IndexViewModel]’
Just create a ViewModel and pass it to your View:
For that to work, you first need to adjust your action method with something like that:
Your View now needs to be strongly typed with the
IndexViewModeltype.Accessing your members would look somewhat like that:
ViewModelsare the way to go when you need to show complex data in your views that doesn’t really project the domain model.