Avatar of manicpreacher
manicpreacher
 asked on

viewmodels

I can't get my head around viewmodels,  which with what I'm trying to do I believe I need.
I'm creating a Blog with a post model (posting of articles)
namespace BlogContent.Models
{
    public class Post
    {
        public int PostID { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
        public string Tag { get; set; }
        public DateTime PostDate { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }
}

Open in new window


and then the model for the comments

namespace BlogContent.Models
{
    public class Comment
    {
        public int CommentID { get; set; }
        public int PostID { get; set; }
        public string name { get; set; }
        public string Body { get; set; }
        public virtual Post Posts { get; set; }
       
    }
}

Open in new window


I can display the list in my view of both the details of the post and all the comments associated with it

controller:
  public ViewResult Details(int id)
        {
            Post post = db.Posts.Find(id);
            return View(post);
        }

Open in new window


and the view
@model BlogContent.Models.Post

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>
@ViewBag.title
<fieldset>
    <legend>Post</legend>

    <div class="display-label">Title</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Title)
    </div>

    <div class="display-label">Body</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Body)
    </div>

    <div class="display-label">PostDate</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.PostDate)
    </div>
    <div class="display-label">
    @Html.LabelFor(model => model.Comments)
</div>
<div class="display-field">
    <table>
        <tr>
            <th>Name</th>
            <th>Body</th>
        </tr>
        @foreach (var item in Model.Comments)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Body)
                </td>
            </tr>
        }
    </table>

</div>
</fieldset>
   <head id="Head1" runat="server">
         <title>AJAX Sample 1</title>
        <script src="<%: Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
    </head>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.PostID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

Open in new window


all works fine, but in the view i want to add the create a new comment section at the bottom... as in my controller
 public ActionResult CreateComment(int id)
        {

            Post post = db.Posts.Find(id);
            return View(post);
        }

Open in new window


and view
@model BlogContent.Models.Comment
@{
    ViewBag.Title = "CreateComment";
}

<h2>CreateComment</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Comment</legend>

       
        <div class="editor-field">
            @Html.HiddenFor(model => model.PostID, new { @Value = ViewBag.post })
            @Html.ValidationMessageFor(model => model.PostID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Body)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Body)
            @Html.ValidationMessageFor(model => model.Body)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Open in new window


I just want to combine them in one view.. do I use partial view, ajax call or viewmodel>?
.NET ProgrammingASP.NETC#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes