MVC 4 - 两个型号的详细页面

时间:2013-07-09 05:50:13

标签: c# asp.net-mvc-4

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

我想要做的是在页面的最底部创建一个表单以添加新评论。由于我使用不同的模型进行评论,因此无法弄清楚如何创建这样的场景。

感谢您的任何建议。

修改 这里有一些代码可以更好地解释:

@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>

3 个答案:

答案 0 :(得分:4)

您使用ViewModel。基本上,它是一切的包装:

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

然后,您的视图采用BlogDetailViewModel类型的模型。您可以使用NewComment作为新的:

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

...等。

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

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

在帖子上,您的NewComment成员将包含新的评论数据。

答案 1 :(得分: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>
}

答案 2 :(得分:0)

将你的评论形成一个部分视图,在某个地方发布并返回到页面,或者做一些AJAX,这可能是更好的选择,因为它提供了更好的用户体验。