this is a response to my earlier question on how to combine multiple models into one...
The thing to do here is to make a view model that contains properties for each of the models you need. ie:
------------------------------------------------
public class ViewModel {
public Model1 Model1 { get; set; }
public Model2 Model2 { get; set; }
}
Then in your action:
var viewModel =newViewModel(); var db =new contentContext(); viewModel.Model1=(from r in db.content...); viewModel.Model2=(from r in db.content...); returnView(viewModel);
-----------------------------------------------
I thought I understood at the time, but I don't understand what the type of Model1 and Model2 are. Are they model classes defined seperately in the models folder?
when I run the action, the return type here viewModel.Model1=(from r
in db.content...); is linq query.
I just want to have multiple dataset available to the view from one controller action.
The type of model1 and model2 all depend on the linq query. The linq query itself either specifies a type or generate an anonymous type. If you want to build a view model you can either change youre linq query to return a pre defined model type. or you can
make your view model have type dynamic for both model1 and model2.
(from r in db.content from cc in r.contentcategories where r.type == "nav" && r.navType == 4 && cc.name.Contains("pet friendly") || cc.name.Contains(categorycookie) select r).Distinct().OrderByDescending(n => n.priority)
These are the important bits of what I have working.
Model Class file in Models folder:
namespace mystuff.Models
{
public class content
{
public int contentid { get; set; }
public string title { get; set; }
public string body { get; set; }
public int priority { get; set; }
public string type { get; set; }
public int navType { get; set; }
public int version { get; set; }
public DateTime createDate { get; set; }
public string createdBy { get; set; }
public DateTime modifiedDate { get; set; }
public string modifiedBy { get; set; }
public virtual ICollection<contentcategory> contentcategories { get; set; }
}
}
Data Context in DAL folder:
namespace mystuff.Models
{
public class businessContext : DbContext
{
public DbSet<content> content { get; set; }
public DbSet<contentcategory> contentcategory { get; set; }
namespace mystuff.Controllers
{
public class HomeController : Controller
{
private businessContext db = new businessContext();
public ActionResult visitors()
{
var model = (from r in db.content from cc in r.contentcategories where r.type == "nav" && r.navType == 4 && cc.name.Contains("pet friendly") || cc.name.Contains(categorycookie) select r).Distinct().OrderByDescending(n => n.priority);
return View(model);
}
}
}
View visitors.cshtml in views/home folder:
@model IEnumerable<mystuff.Models.content>
@foreach (var item in Model)
{
@Html.Partial("_tier4Nav", item)
}
So this works. It is used to build navigation items. Now I need a second tier of nav items - e.g. main nav and secondary nav, so I need a different dataset, and I'm thinking that's the result of another linq query.
Further, I will need to make more content - like a single result of a query - which will not be a list. In other words, I foresee my next problem being the data view is enumerable, and in some cases, the data I want will just be one thing, tlike a details
page. But it won't be on a seperate page, it will be on the same page as the navs (multiple lists of things). Maybe we should stick to one question at a time though.
Creating a View Model for the Instructor Index View
The Instructor Index page shows three different tables. Therefore, you'll create a view model that includes three properties, each holding the data for one of the tables.
In the ViewModels folder, create InstructorIndexData.cs and replace the existing code with the following code:
using System;
using System.Collections.Generic;
using ContosoUniversity.Models;
namespace ContosoUniversity.ViewModels
{
public class InstructorIndexData
{
public IEnumerable<Instructor> Instructors { get; set; }
public IEnumerable<Course> Courses { get; set; }
public IEnumerable<Enrollment> Enrollments { get; set; }
}
}
This is the section of the ef tutorial that I think corresponds to our discussion. Sound right?
Yes.
View Model is differ from Model. It is just a convenient class for passing data between the controller and the view. As the view data class exposes only one Model property. So you need a View Model when you need to pass multiple data items from a controller
to a view.
Hope this helpful
Regards
Young Yang
Please mark the replies as answers if they help or unmark if not.
Feedback to us
jypelton
Member
186 Points
162 Posts
what are the types of these properties in my model
Apr 25, 2012 08:54 PM|LINK
this is a response to my earlier question on how to combine multiple models into one...
The thing to do here is to make a view model that contains properties for each of the models you need. ie:
------------------------------------------------
public class ViewModel { public Model1 Model1 { get; set; } public Model2 Model2 { get; set; } }Then in your action:
-----------------------------------------------
I thought I understood at the time, but I don't understand what the type of Model1 and Model2 are. Are they model classes defined seperately in the models folder?
when I run the action, the return type here viewModel.Model1 = (from r in db.content...); is linq query.
I just want to have multiple dataset available to the view from one controller action.
CodeHobo
All-Star
18669 Points
2648 Posts
Re: what are the types of these properties in my model
Apr 25, 2012 08:57 PM|LINK
The type of model1 and model2 all depend on the linq query. The linq query itself either specifies a type or generate an anonymous type. If you want to build a view model you can either change youre linq query to return a pre defined model type. or you can make your view model have type dynamic for both model1 and model2.
What does your complete linq query look like?
Blog | Twitter : @Hattan
jypelton
Member
186 Points
162 Posts
Re: what are the types of these properties in my model
Apr 25, 2012 09:33 PM|LINK
Glad you're here codehobo.
this is the linq query
(from r in db.content from cc in r.contentcategories where r.type == "nav" && r.navType == 4 && cc.name.Contains("pet friendly") || cc.name.Contains(categorycookie) select r).Distinct().OrderByDescending(n => n.priority)
These are the important bits of what I have working.
Model Class file in Models folder:
namespace mystuff.Models
{
public class content
{
public int contentid { get; set; }
public string title { get; set; }
public string body { get; set; }
public int priority { get; set; }
public string type { get; set; }
public int navType { get; set; }
public int version { get; set; }
public DateTime createDate { get; set; }
public string createdBy { get; set; }
public DateTime modifiedDate { get; set; }
public string modifiedBy { get; set; }
public virtual ICollection<contentcategory> contentcategories { get; set; }
}
}
Data Context in DAL folder:
namespace mystuff.Models
{
public class businessContext : DbContext
{
public DbSet<content> content { get; set; }
public DbSet<contentcategory> contentcategory { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<content>()
.HasMany(c => c.contentcategories).WithMany(i => i.content)
.Map(t => t.MapLeftKey("contentid")
.MapRightKey("contentcategoryid")
.ToTable("joincontentcategory"));
}
}
}
Controller:
namespace mystuff.Controllers
{
public class HomeController : Controller
{
private businessContext db = new businessContext();
public ActionResult visitors()
{
var model = (from r in db.content from cc in r.contentcategories where r.type == "nav" && r.navType == 4 && cc.name.Contains("pet friendly") || cc.name.Contains(categorycookie) select r).Distinct().OrderByDescending(n => n.priority);
return View(model);
}
}
}
View visitors.cshtml in views/home folder:
@model IEnumerable<mystuff.Models.content>
@foreach (var item in Model)
{
@Html.Partial("_tier4Nav", item)
}
------------------------------------------------------------
So this works. It is used to build navigation items. Now I need a second tier of nav items - e.g. main nav and secondary nav, so I need a different dataset, and I'm thinking that's the result of another linq query.
Further, I will need to make more content - like a single result of a query - which will not be a list. In other words, I foresee my next problem being the data view is enumerable, and in some cases, the data I want will just be one thing, tlike a details page. But it won't be on a seperate page, it will be on the same page as the navs (multiple lists of things). Maybe we should stick to one question at a time though.
jypelton
Member
186 Points
162 Posts
Re: what are the types of these properties in my model
Apr 26, 2012 01:32 PM|LINK
This is the section of the ef tutorial that I think corresponds to our discussion. Sound right?
from:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
Creating a View Model for the Instructor Index View
The Instructor Index page shows three different tables. Therefore, you'll create a view model that includes three properties, each holding the data for one of the tables.
In the ViewModels folder, create InstructorIndexData.cs and replace the existing code with the following code:
using System; using System.Collections.Generic; using ContosoUniversity.Models; namespace ContosoUniversity.ViewModels { public class InstructorIndexData { public IEnumerable<Instructor> Instructors { get; set; } public IEnumerable<Course> Courses { get; set; } public IEnumerable<Enrollment> Enrollments { get; set; } } }Young Yang -...
All-Star
21741 Points
1825 Posts
Microsoft
Re: what are the types of these properties in my model
Apr 27, 2012 10:31 AM|LINK
Hi
Yes.
View Model is differ from Model. It is just a convenient class for passing data between the controller and the view. As the view data class exposes only one Model property. So you need a View Model when you need to pass multiple data items from a controller to a view.
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store