开发者

Pass parameter when creating new Model

I have a view that is sho开发者_JAVA百科wing details of my post with comments, it also rendering a partial view that is responsible for comment adding. The problem is that I don't know how to pass parameter with new Comment() with already existing value in it. That is PostID.

model is Post model that is being used for rendering post data like Body/Title etc...

My code: @Html.Partial("_CommentAdd", new Comment())

What I want to do: @Html.Partial("_CommentAdd", new Comment({ PostID = model.ID}))

Full Code

@using project01.Models
@model project01.Models.Post
@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<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">Tags</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Tags)
    </div>

    <div class="display-label">Date</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Date)
    </div>
</fieldset>

@Html.Partial("_CommentAdd", new Comment())

@foreach (var comment in Model.Comments)
{
    @Html.Partial("_Comment", comment)
}

Picture with arrow that is showing what I want to achieve.

Pass parameter when creating new Model


Is your syntax just a bit off? Try this (the property intializers go after the constructor, not as "arguments" to the constructor call):

@Html.Partial("_CommentAdd", new Comment() { PostID = model.ID })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜