To make things simple, I have 3 entities (one to many, left to right):
COURSE -> MODULE -> CHAPTER
A course can have multiple modules and a module can have multiple chapters.
All I have in the controller is this:
Course course = db.Courses.Find(id);
return View(course);
I was trying to use an include but it doesn't seem to evaluate (this doesn't seem to work: Course course = db.Courses.Include("Modules").Find(id);)
So I let it be. In my view, I have this nested List:
<ul>
#foreach (var item in Model.Modules)
{
<li>#module.Title
<ul>
#foreach (var item in module.Chapters)
{
<li>#chapter.Title</li>
}
</ul>
</li>
}
</ul>
Will this work automatically?
Lastly, I applied a SortOrder column so I could arrange the child entities. I know that this should be done in the query, but how can I do this?
Thanks! Any piece of information or advise would be highly appreciated.
UPDATE
Course Class
public partial class Course
{
public Course()
{
this.Modules = new HashSet<Module>();
this.Assets = new HashSet<Asset>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Author { get; set; }
public System.DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public int IndustryId { get; set; }
public virtual ICollection<Module> Modules { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
public virtual Industry Industry { get; set; }
}
Module Class
namespace RocketLabs.Models
{
using System;
using System.Collections.Generic;
public partial class Module
{
public Module()
{
this.Chapters = new HashSet<Chapter>();
this.Assets = new HashSet<Asset>();
}
public int Id { get; set; }
public string Title { get; set; }
public System.DateTime CreateDate { get; set; }
public int CourseId { get; set; }
public bool IsDeleted { get; set; }
public short SortOrder { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Chapter> Chapters { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
public virtual Exam Exam { get; set; }
}
}
Chapter Class
namespace RocketLabs.Models
{
using System;
using System.Collections.Generic;
public partial class Chapter
{
public Chapter()
{
this.Assets = new HashSet<Asset>();
}
public int Id { get; set; }
public string Title { get; set; }
public int ModuleId { get; set; }
public string Notes { get; set; }
public short SortOrder { get; set; }
public System.DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public virtual Module Module { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
}
}
you can do this:
var course = db.Courses
.Include(i => i.Modules.Select(s => s.Chapter))
.Single(s=>s.Id == id);
return View(course);
And your loop:
<ul>
#foreach (var module in Model.Modules)
{
<li>#module.Title
<ul>
#foreach (var chapter in module.Chapters)
{
<li>#chapter.Title</li>
}
</ul>
</li>
}
</ul>
As for sorting, we can include .OrderBy() on your query but you need to elaborate what child entities you want sorted and by what fields.
Related
I'm trying to create a library of books.
I separated data and users context/database for security reasons (and clarity) but is it that useful?
Specially since BookOfUserEntity.Id should be really a composite key between UserId & BookId.
Something like :
public class BookOfUserEntity
{
[Key]
public Guid UserId { get; set; }
[Key]
public int BookId { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BookOfUserEntity>()
.HasKey(b => new { b.UserId, b.BookId });
}
But then if I create a composite key what should be HistoryEntity.BookOfUserEntityId type & value?
I also separated BookEntity & BookOfUserEntity to avoid duplicated data and reuse what can be reused (MainCoverArtId etc...), but this added complexity - should I go back to a simpler model?
public class BookEntity
{
public int Id { get; set; }
public Guid? MainCoverArtId { get; set; }
...
public virtual List<BookOfUserEntity>? BookOfUserEntities { get; set; } // Only purpose is to know how many Users have this very book in their list.
}
public class BookOfUserEntity // Bad name here don't mention it
{
public int Id { get; set; }
public Guid UserId { get; set; }
public int BookId { get; set; }
public BookEntity Book { get; set; } // Useful? Should be virtual?
public List<HistoryEntity>? HistoryEntities { get; set; }
...
}
public class HistoryEntity
{
public int Id { get; set; }
public int BookOfUserEntityId { get; set; }
public BookOfUserEntity BookOfUserEntity { get; set; } // Same questions
public int Vol { get; set; }
public double Chapter { get; set; }
public DateTime? ReadDate { get; init; }
}
I am trying to get this expression working in my Razor file:
<span>#($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")</span>
But it says "Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed" in the red squiggly in the Razor and the Developer exception page.
Here is my model (#model CommentVM) coming down to the Razor from the controller action:
public class CommentVM
{
public int AuditId { get; set; }
public string Comment { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
Here is the Controller Action (actually this is coming from the Invoke method in a component. But this shouldn't really make a difference).
public IViewComponentResult Invoke(int auditId)
{
IQueryable<Comment> comments = _commentRepo.Comments.Include(c => c.CommentStaff).ThenInclude(c => c.StaffOffices)
.Where(c => c.CommentAuditId == auditId)
.OrderByDescending(c => c.CommentDate).Take(3);
CommentVM commentVM = new CommentVM
{
AuditId = auditId,
Comments = comments
};
return View(commentVM);
}
The Comments Repo returns an IQueryable of Comment objects.
The Comment POCO looks like this:
[Table("comment")]
public class Comment
{
[Key]
[Column("comment_id")]
public int CommentId { get; set; }
[Column("comment_type_cd")]
public string CommentTypeCd { get; set; }
[Column("comment_audit_id")]
public int? CommentAuditId { get; set; }
[Column("comment_finding_id")]
public int? CommentFindingId { get; set; }
[Column("comment_recommend_id")]
public int? CommentRecommendId { get; set; }
[Column("comment_action_item_id")]
public int? CommentActionItemId { get; set; }
[Column("comment_acd_id")]
public int? CommentAcdId { get; set; }
[Column("comment_pdl_id")]
public int? CommentPdlId { get; set; }
[Column("comment_cost_nm")]
public string CommentCostNm { get; set; }
[Required]
[Column("comment_tx")]
public string CommentText { get; set; }
[Column("comment_dt")]
public DateTime CommentDate { get; set; }
#region Navigation Properties
[Column("comment_staff_id")]
public short CommentStaffId { get; set; }
[ForeignKey("CommentStaffId")]
public Staff CommentStaff { get; set; }
#endregion
}
So CommentStaff is a navigation property to a Staff object which has a bridge table POCO called StaffOffices for a many to many relationship between Staff and Office.
Here is the bridge table POCO:
[Table("staff_office")]
public class StaffOffice
{
[Column("staff_office_staff_id")]
public short ID { get; set; }
public Staff Staff { get; set; }
[Column("staff_office_office_id")]
public short OfficeID { get; set; }
public Office Office { get; set; }
}
I am trying to get this:
<span>#($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")</span>
or this:
#{
StringBuilder sb = new StringBuilder();
foreach(var o in comment.CommentStaff.StaffOffices)
{
sb.Append(o.Office.OfficeOrganizationCd);
}
}
<span>#sb.ToString()</span>
It's a partial view. I needed to include the model definition of the parent Razor file:
#model CommentVM
I am (trying to) build a multiple choice test application that will pass a list of multiple choice questions (select lists) from the controller to a view.
Then populate the view using a foreach loop, post the answers back to the controller, check them and increment the score for each correct answer and then update the db.
I am trying to populate the view model list using a Linq query (to keep my controller thin, I am doing this via a method in my Service layer).
Models
Questions (db first)
namespace AccessEsol.Models
{
using System;
using System.Collections.Generic;
public partial class Question
{
public Question()
{
this.ExamDets = new HashSet<ExamDet>();
}
public int QuID { get; set; }
public string Text1 { get; set; }
public string Text2 { get; set; }
public string CorrectAnswer { get; set; }
public string Foil1 { get; set; }
public string Foil2 { get; set; }
public string Foil3 { get; set; }
public string Level_ { get; set; }
public string GrammarPoint { get; set; }
public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}
Exam
namespace AccessEsol.Models
{
using System;
using System.Collections.Generic;
public partial class Exam
{
public Exam()
{
this.Candidates = new HashSet<Candidate>();
this.ExamDets = new HashSet<ExamDet>();
}
public int ExamID { get; set; }
public string Name { get; set; }
public Nullable<System.DateTime> Date { get; set; }
public int AdminID { get; set; }
public string StartLevel { get; set; }
public virtual Administrator Administrator { get; set; }
public virtual ICollection<Candidate> Candidates { get; set; }
public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}
There is also a model for Candidate that contains the CandID:
And the view model that uses these models:
namespace AccessEsol.Models
{
public class ExamQuestionsViewModel
{
public int QuID { get; set; }
public int CandID { get; set; }
public int ExamId { get; set; }
public string Text1 { get; set; }
public string Text2 { get; set; }
public string CorrectAnswer { get; set; }
public string Foil1 { get; set; }
public string Foil2 { get; set; }
public string Foil3 { get; set; }
public string Level { get; set; }
public string GrammarPoint { get; set; }
public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}
This is the method that is to populate the view model:
public static List<ExamQuestionsViewModel> AddQuestions()
{
AccessEsolDataEntities db = new AccessEsolDataEntities();
string questionLevel = GetLevel();
int currentCand = GetCandID();
int currentExam = GetExamID();
//model = new DataAccess().Populate();
var qu = (from a in db.Questions
where a.Level_ == questionLevel
select a).ToList();
List<ExamQuestionsViewModel> exam = new List<ExamQuestionsViewModel>();
foreach (var IDs in exam)
{
currentCand = exam.CandID;
currentExam = exam.ExamId;
}
return (exam);
}
The error message I am getting is
'System.Collections.Generic.List<AccessEsol.Models.ExamQuestionsViewModel>'
does not contain a definition for 'ExamId' and no extension method
'ExamId' accepting a first argument of type
'System.Collections.Generic.List<AccessEsol.Models.ExamQuestionsViewModel>' could be found (are you missing a using directive or an assembly
reference?
What am I doing wrong here? All feedback much appreciated.
Please try this instead of your foreach:
foreach (var IDs in exam)
{
currentCand = IDs.CandID;
currentExam = IDs.ExamId;
}
I'm creating a page that will display 6 blogs that were created in the passed 7 days. The blog contains an image and multiple comments
Here is the Blog model
public class Blog
{
public int BlogID { get; set; }
public int likes { get; set; }
public int ImageID { get; set; }
public Boolean removed { get; set; }
public DateTime dte_created { get; set; }
public DateTime? dte_modified { get; set; }
public virtual Image Image { get; set; }
public virtual ICollection<Comment> Comment { get; set; }
}
And here's the blogContent.
public class Image
{
public int ImageID { get; set; }
public string img_path { get; set; }
public string Description { get; set; }
public DateTime dte_created { get; set; }
}
public class Comment
{
public int CommentID { get; set; }
public string Commentation { get; set; }
public int likes { get; set; }
public int BlogID { get; set; }
public DateTime dte_created { get; set; }
public DateTime? dte_modified { get; set; }
public Boolean removed { get; set; }
//public virtual int Account_ID { get; set; }
public virtual Blog Blog { get; set; }
}
Here is the controller
private ACaptureDB db = new ACaptureDB();
public ViewResult Index()
{
ViewBag.Message = "ArchiCapture";
var dateCheck = DateTime.Now.AddDays(-7);
var results = from r in db.Blog
where r.dte_created >= dateCheck
select r;
return View(results);
}
and my View.
#model IEnumerable<ACapture.Models.Blog>
#{
ViewBag.Title = "Home Page";
}
<div class="Forum">
<p>The Forum</p>
<form class="Forum" runat="server">
#foreach (var item in Model)
{
<div class="ForumChild"><img src="#item.Image.img_path" alt="Not Found" />
<br />
<table>
?????????
</table>
</div>
}
</form>
</div>
How would I retrieve all the comments that are linked to the blog?
You should be able to loop over the Comments collection in your model blog object:
<table>
#foreach(var comment in Model.Comments) {
<tr>
<td>#comment.Commentation</td>
....
</tr>
}
</table>
I'm building website in MVC 3.
i am using EF code first in existing database.
my ET inside the model look like that:
public class Pages
{
[Required]
public int ID { get; set; }
public int ParentID { get; set; }
[Required]
public int PageType { get; set; }
[Required]
[DataType(DataType.Text)]
[DisplayName("כותרת")]
public string Title { get; set; }
public string SearchWords { get; set; }
public string Leng { get; set; }
public int? Sort { get; set; }
public string Modules { get; set; }
[ForeignKey("PageType")]
public virtual PagesType Type { get; set; }
public virtual IEnumerable<PagesType> Types { get; set; }
[ForeignKey("PageID")]
public ICollection<PageContent> PageContent { get; set; }
[ForeignKey("PageID")]
public virtual ICollection<ImagesTable> Images { get; set; }
}
public class PageContent
{
public int ID { get; set; }
public int PageID { get; set; }
public string Header { get; set; }
public string Text { get; set; }
[ForeignKey("ID")]
public virtual ICollection<Pages> Pages { get; set; }
}
as you see in my fist table that cold Pages i have a relationship to another table that named PageContent.
in my Pages class i had this code
[ForeignKey("PageID")]
public ICollection<PageContent> PageContent { get; set; }
now, when i trying to add new pageContent into new page i get an error.
see this code
public ActionResult AddPage(PageModel page)
{
SystemLogic cmd = new SystemLogic();
page.Leng = "he";
Models.Pages p = new Pages();
p.ParentID = page.ParentID;
PageContent pageContent = new PageContent();
pageContent.Text = page.Content;
p.PageContent.Add(pageContent);
The error is
Object reference not set to an instance of an object.
What i did wrong?
You will get the NRE at p.PageContent.Add(pageContent); because the collection is not initialized. Initialize the collections inside the constructor of Pages class.
public class Pages
{
public Pages()
{
PageContent = List<PageContent>();
Images = List<ImagesTable>();
}
[Required]
public int ID { get; set; }
public int ParentID { get; set; }
[Required]
public int PageType { get; set; }
[Required]
[DataType(DataType.Text)]
[DisplayName("כותרת")]
public string Title { get; set; }
public string SearchWords { get; set; }
public string Leng { get; set; }
public int? Sort { get; set; }
public string Modules { get; set; }
[ForeignKey("PageType")]
public virtual PagesType Type { get; set; }
public virtual IEnumerable<PagesType> Types { get; set; }
[ForeignKey("PageID")]
public ICollection<PageContent> PageContent { get; set; }
[ForeignKey("PageID")]
public virtual ICollection<ImagesTable> Images { get; set; }
}
Or before you add objects to the collection
if (p.PageContent == null) p.PageContent = new List<PageContent>();
p.PageContent.Add(pageContent);
You should consider using proper naming conventions(eg Page instead of Pages).