Link to home
Create AccountLog in
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>?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account