Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
}
like image 714
Ali Paeizi Avatar asked Nov 13 '22 10:11

Ali Paeizi


1 Answers

Create a new model error corrected

public class PageViewModel
    {
        public PageModel PageModel { set; get; }
        public CommentModel CommentModel { set; get; }
    }
like image 152
Ali Paeizi Avatar answered Nov 15 '22 00:11

Ali Paeizi