MVC核心。模板只能与字段访问,属性访问,一维数组索引或单参数自定义索引器表达式一起使用

时间:2018-11-28 19:10:08

标签: asp.net-mvc asp.net-core-tag-helpers

我正在通过此模型

public class BlogViewModel
{
    public List<CommentModel> commentModels { get; set; }
    public BlogPostLayoutModel blogPostLayoutModel { get; set; }
    public CommentModel commentModel { get; set; }
    public int pageNumber { get; set; }
}

CommentModel类

 public class CommentModel
{
    public int IDComment { get; set; }
    public int IDPost { get; set; }

    [Required()]
    public string Username { get; set; }
    [Required()]
    public string Content { get; set; }

    public static CommentModel CommentToCommentModel(Comment comment)
    {
        CommentModel commentModel = new CommentModel();
        commentModel.IDComment = comment.IDComment;
        commentModel.IDPost = comment.IDPost;
        commentModel.Username = comment.Username;
        commentModel.Content = comment.Content;
        return commentModel;
    }

    public static List<CommentModel> CommentsToCommentModels(List<Comment> comments)
    {
        List<CommentModel> commentModels = new List<CommentModel>();
        foreach (var comment in comments)
        {
            CommentModel commentModel = new CommentModel();
            commentModel.IDComment = comment.IDComment;
            commentModel.IDPost = comment.IDPost;
            commentModel.Username = comment.Username;
            commentModel.Content = comment.Content;
            commentModels.Add(commentModel);
        }
        return commentModels;
    }
}

我的观点是在哪里引发错误(第五行)

<form asp-action="AddComment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        @{ var pageNumber = Model.pageNumber;
        @Html.HiddenFor(x => pageNumber)
            int IDPost = Model.blogPostLayoutModel.IDPost;
        @Html.HiddenFor(x => x.commentModel.IDPost == IDPost)
        }

        <div class="row">
            <div class="form-group col-md-4">
                <label asp-for="commentModel.Username" class="control-label"></label>
                <input asp-for="commentModel.Username" class="form-control" />
                <span asp-validation-for="commentModel.Username" class="text-danger"></span>
            </div>
        </div>
        <div class="row">
            <div class="form-group col-md-12">
                <label class="control-label">Comment</label>
                <textarea asp-for="commentModel.Content" class="form-control"></textarea>
                <span asp-validation-for="commentModel.Content" class="text-danger"></span>
            </div>
            <div class="form-group col-md-4">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </form>

我已经搜索了错误,但是似乎大多数人都在尝试从方法中返回一个值,并被告知将值传递给局部变量,然后将该局部变量放入标记帮助器中,但这并没有。

blogPostLayoutModel.IDPost也定义为

 public int IDPost { get; set; }

编辑:我的commentModel为空,所以是问题所在。

工作表格

<form asp-action="AddComment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        @{
        @Html.HiddenFor(x => Model.pageNumber)
            Model.commentModel.IDPost = Model.blogPostLayoutModel.IDPost;
        @Html.HiddenFor(x => x.commentModel.IDPost)
        }
        <div class="row">
            <div class="form-group col-md-4">
                <label asp-for="commentModel.Username" class="control-label"></label>
                <input asp-for="commentModel.Username" class="form-control" />
                <span asp-validation-for="commentModel.Username" class="text-danger"></span>
            </div>
        </div>
        <div class="row">
            <div class="form-group col-md-12">
                <label class="control-label">Comment</label>
                <textarea asp-for="commentModel.Content" class="form-control"></textarea>
                <span asp-validation-for="commentModel.Content" class="text-danger"></span>
            </div>
            <div class="form-group col-md-4">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </form>

0 个答案:

没有答案