1

我正在尝试在页面底部创建一个带有评论的博客详细信息页面。我使用我的“发布”模型作为我评论的一些属性和导航列表属性。

我想做的是在页面的最底部创建一个表单来添加新评论。由于我对评论使用了不同的模型,所以我很难弄清楚如何创建这样的场景。

感谢您的任何建议。

编辑 这里有一些代码可以更好地解释:

@model Skuami.Models.PostModel
@{
    ViewBag.Title = Model.Title;
}

<div class="blogContentBox">
    <div class="blogImage">
        <img src="@Model.Image" />
    </div>
    <div class="blogContent">
        <div class="blogDate">@Model.CreateDate</div>
        <p class="blogTitle">@Model.Title</p>
        @Model.Content
    </div>
</div>

<p class="commentTitle">
    @Model.Comments.Count @(Model.Comments.Count == 1 ? " comment" : " comments")
</p>

@foreach (var item in Model.Comments)
{
    <div class="commentBox">
        <p class="commentName">@item.Name</p>
        <p class="commentDate">@item.CreateDate</p>
        @item.Message
    </div>
}

<div class="commentBox">
    <p class="commentReply">Leave a Reply</p>

     @using (Html.BeginForm("Details", "Home", FormMethod.Post, new { @class = "CommentModel" }))
    {

        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend></legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.Comments.Name) <!-- how can I use a 2nd model -->
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name) <!-- how can I use a 2nd model -->
                @Html.ValidationMessageFor(model => model.Name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Message) <!-- how can I use a 2nd model -->
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Message) <!-- how can I use a 2nd model -->
                @Html.ValidationMessageFor(model => model.Message)
            </div>

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

3 回答 3

4

你使用一个ViewModel. 基本上,它是所有内容的包装器:

public class BlogDetailViewModel {
    public Post Post { get; set; }
    public IList<Comment> Comments { get; set; }
    public Comment NewComment { get; set; }
}

然后,您的视图采用 type 的模型BlogDetailViewModel。您将使用NewComment新的:

@Html.TextBoxFor(x => x.NewComment.Email)
@Html.TextBoxFor(x => x.NewComment.CommentContent)

..ETC。

在您的控制器中..您构建它:

return View(new BlogDetailViewModel() { Post = thePostVariable, Comments = getCommentsForPost(), NewComment = new Comment() });

在发布时,您的NewComment成员将包含新的评论数据。

于 2013-07-09T05:53:35.580 回答
0

使您的评论形成一个局部视图,在某处发布并返回页面,或者执行一些 AJAX,这可能是这里更好的选择,因为它可以提供更好的用户体验。

于 2013-07-09T07:29:30.037 回答
0

模型

  public class CustomerModel
    {
        public int CustomerId { get; set; }

        public string customerName { get; set; }

        public List<SelectListItem> customerNameList { get; set; }
}



  public class RegisterModel
    {
        public int ID { get; set; }


        public string Name { get; set; }


        public string PhoneNo { get; set; }


        public string Address { get; set; }

        public CustomerModel _Customer { get; set; }


    }

看法

@model TwoModelInSinglePage.Models.RegisterModel
@{
    Layout = null;
}
  @using (Html.BeginForm("Index", "Content", FormMethod.Post, new { id = "FrmIndex" }))
        { 
            <div>
                @Html.LabelFor(m => m.Name)
                @Html.TextBoxFor(m => m.Name)
                @Html.ValidationMessageFor(m => m.Name)
            </div>
            <br />
            <div>
                @Html.LabelFor(m => m.Address)
                @Html.TextBoxFor(m => m.Address)
                @Html.ValidationMessageFor(m => m.Address)
            </div>
            <br />
            <div>
                @Html.LabelFor(m => m.PhoneNo)
                @Html.TextBoxFor(m => m.PhoneNo)
                @Html.ValidationMessageFor(m => m.PhoneNo)
            </div>
           <div>
                @Html.LabelFor(m => m._Customer.customerName)
            @Html.TextBoxFor(m => m._Customer.customerName)
            @Html.ValidationMessageFor(m => m._Customer.customerName)
            </div>
}
于 2013-07-09T06:10:36.430 回答