如何在剃刀语法中组合嵌套模型 lambda

How to combine nested model lambdas in razor syntax

我的视图模型

public class PostViewModel
{
    public Post Post { get; set; }

    public IEnumerable<Comment> Comments { get; set; }
}

在我的剃刀视图中,我正在尝试获取评论数据

@using (Html.BeginForm("Comment", "Post"))
{


    <div class="form-group">
        @Html.LabelFor(m => m.Comments.Data);
        @Html.TextAreaFor(m => m.Comments.Data, new { @class = "form-control" })
    </div>

    @Html.HiddenFor(m => m.Comments.Id)
    <button type="submit" class="btn btn-primary"> Comment </button>
}

但是我遇到了错误

然后我尝试了以下语法

@using (Html.BeginForm("Comment", "Post"))
{

    @foreach(var comment in Model.Comments)
    {
        <div class="form-group">
            @Html.LabelFor(comment.Data);
            @Html.TextAreaFor(comment.Data, new { @class = "form-control" })
        </div>

        @Html.HiddenFor(comment.Id)
        <button type="submit" class="btn btn-primary"> Comment </button>

    }
}

但我仍然遇到错误

其实我在做一个博客项目

所以,我希望 Post,详细信息页面中有所有旧评论和新评论按钮

您的 lambda 语法错误。以下将编译和工作,但值不会回发到控制器操作:

@foreach(var comment in Model.Comments)
{
    @Html.LabelFor(x=> comment.Data)
}

其次,为了将集合发布回操作,它应该在一个 for 循环中完成,索引名为控件,因为模型绑定器使用输入控件的名称将其绑定回集合,该名称不会以以下格式生成模型活页夹需求。

喜欢:

@for(int i=0; i < Model.Comments.Count(); i++)
{
    <div class="form-group">
        @Html.LabelFor(x => Model.Comments[i].Data);
        @Html.TextAreaFor(x => Model.Comments[i].Data, new { @class = "form-control" })
    </div>

    @Html.HiddenFor(x => Model.Comments[i].Id)
    <button type="submit" class="btn btn-primary"> Comment </button>

}

Ehsan Sajjad 让我明白我们可以这样写 lambda 表达式

@Html.LabelFor(x=> comment.Data)

后来我解决了我的问题,其实我的做法是错误的

为了解决我的问题,我在我的 ViewModel 中添加了另一个组件 NewComment

public class PostViewModel
{
    public Post Post { get; set; }

    public IEnumerable<Comment> Comments { get; set; }

    public Comment NewComment { get; set; } // this is new addition
}

然后我的新评论区就像razor语法中的下面

@using (Html.BeginForm("Comment", "Post", Model.Post))
{


    var comment = Model.NewComment;

    <div class="form-group">
        @Html.LabelFor(m => comment.Data);
        @Html.TextAreaFor(m => comment.Data, new { @class = "form-control" })
    </div>

    @Html.HiddenFor(m => comment.Id)
    <button type="submit" class="btn btn-primary"> Comment </button>
}

我正在做一个项目,在详细信息视图中

首先,出现了一个Post

其次是评论

三、新评论部分

详情页面的完整代码

@model SimpleBlog.Models.PostViewModel

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@*Post Section*@

<div class="jumbotron">
    <h1> @Model.Post.Title </h1>
    <p class="lead"> @Model.Post.PostedBy </p>
    <p> @Model.Post.PostDate.ToString("d MMM yyyy") </p>

</div>

<br />

<div class="jumbotron">

    <p class="lead"> @Model.Post.Body </p>

</div>


@* Old comments section *@

@foreach (var comment in Model.Comments)
{

    <h4>  @comment.CommentBy  </h4>
    <h4>  @comment.CommentDate.ToString("d MMM yyyy") </h4>
    <h4> @comment.Data  </h4>

    <br />
    <br />

}

@* New Comment section *@

@using (Html.BeginForm("Comment", "Post", Model.Post))
{


    var comment = Model.NewComment;

    <div class="form-group">
        @Html.LabelFor(m => comment.Data);
        @Html.TextAreaFor(m => comment.Data, new { @class = "form-control" })
    </div>

    @Html.HiddenFor(m => comment.Id)
    <button type="submit" class="btn btn-primary"> Comment </button>
}