troubleshooting Question

viewmodels

Avatar of manicpreacher
manicpreacher asked on
.NET ProgrammingC#ASP.NET
1 Comment1 Solution292 ViewsLast Modified:
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; }
    }
}

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; }
       
    }
}

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);
        }

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>

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);
        }

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>

I just want to combine them in one view.. do I use partial view, ajax call or viewmodel>?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros