How do I display data from multiple tables on a page in MVC?

How do I display data from multiple tables on a page in MVC?

My models:

This is page model

public class PageModel
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int PageId { get; set; }

        public string PageTitle { get; set; }

        public string PageSubject { get; set; }

        public DateTime CreateDate { get; set; }

        public DateTime UpdateDate { get; set; }

        public int ParentId { get; set; }

        public bool IsActive { get; set; }

        public IList<PageContentModel> PageContentModels { get; set; }
        public IList<CommentModel> CommentModels { get; set; }
    }

This is content model

 public class PageContentModel
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ContentId { get; set; }

        public int PageId { get; set; }
        public virtual PageModel PageModel { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        [MaxLength(1024)]
        public string Content { get; set; }
    }

This comment model

public class CommentModel
        {
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int CommentId { get; set; }

            public string CommentAuthor { get; set; }

            public string CommentContent { get; set; }

            public DateTime CreateDate { get; set; }

            public bool IsActive { get; set; }

            public int PageId { get; set; }
            public virtual PageModel PageModel { get; set; }
        }

I wrote the following

But it gives an error message when registering comment

Error message: No parameterless constructor defined for this object

@model Tuple<DynamicWebsite.Models.PageModel, DynamicWebsite.Models.CommentModel>
@helper GetParentTitle(int id)
{
    if (id != 0)
    {
        DynamicWebsite.DataLayer.EFDbContext x = new DynamicWebsite.DataLayer.EFDbContext();
        var item = x.PageModels.Find(id);
        x.Dispose();
    @GetParentTitle(item.ParentId);
    <span>| </span><a href="@item.PageId">@item.PageTitle</a>

    }
    else
    {
    @Html.ActionLink("Home", "Index", "Home")}
}
@GetParentTitle(Model.Item1.PageId)
@if (Model.Item1.IsActive)
{

    ViewBag.Title = Model.Item1.PageTitle;

    <fieldset>
        <legend>PageModel</legend>

        <div class="display-label">
            Page Subject:<h2>@Model.Item1.PageSubject</h2>
        </div>
        <p>
            @foreach (var item in Model.Item1.PageContentModels)
            {
                @item.Content
            }
        </p>
        @if (Model.Item1.UpdateDate > Model.Item1.CreateDate)
        {
            <h5>Create Date: @String.Format("{0:g}", Model.Item1.CreateDate)</h5>
            <h5>UpdateDate: @String.Format("{0:g}", Model.Item1.UpdateDate)</h5>
        }
        else
        {
            <h5>Create Date: @String.Format("{0:g}", Model.Item1.CreateDate)</h5>
        }
        @foreach (var item in Model.Item1.CommentModels)
        {
            if (item.IsActive)
            {
            <h3>@item.CommentAuthor said:</h3>
            <p>@item.CommentContent</p>
            <h5>Create Date: @String.Format("{0:g}", item.CreateDate)</h5>
            }
        }
    </fieldset>

    using (Html.BeginForm())
    {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>CommentModel</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Item2.CommentAuthor)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Item2.CommentAuthor)
                @Html.ValidationMessageFor(model => model.Item2.CommentAuthor)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Item2.CommentContent)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Item2.CommentContent)
                @Html.ValidationMessageFor(model => model.Item2.CommentContent)
            </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    }



}
else
{
    ViewBag.Title = "Deactive";
    <p>This page deactive.</p>
}

Answers


Create a new model error corrected

public class PageViewModel
    {
        public PageModel PageModel { set; get; }
        public CommentModel CommentModel { set; get; }
    }

Need Your Help

Objective-C RC4 Decryption

objective-c ios5 encryption-symmetric rc4-cipher

I am new to Objective-C, but am an experienced developer (C#), but I can't figure this out:

Adding the text in a text box to a database table using OData and Knockout Js

javascript sql html5 knockout.js odata

I am trying to add a value in some drop downs and text boxes to a table in sql database. Its works fine with the drop downs but with textboxes it adds NULL values to the table columns even though I...