如何在视图中设置模型参数值

时间:2013-01-07 12:23:19

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

<div class="forms-box">
   <div class="inputs">
       @Html.HiddenFor(model => model.AddNewComment.CommentParentID)
       @Html.LabelFor(model => model.AddNewComment.CommentText)
       <div class="input-box">
          @Html.TextAreaFor(
               model => model.AddNewComment.CommentText,
                new { @class = "comment-text" })
        </div>
        @Html.ValidationMessageFor(model => model.AddNewComment.CommentText)
   </div>

在代码中:

我从两个

获得一个值
  1. Model.AddNewComment.CommentParentID或

  2. Model.AddNewComment.CommentText

  3. 在我的控制器中。

      public ActionResult BlogCommentReply(
             int blogPostId,
             BlogPostModel model, 
             bool captchaValid)
     {
    
     }
    

1 个答案:

答案 0 :(得分:0)

解决方案: model.AddNewComment.CommentText字段是强制性的。  所以我只是做了以下事情。

 @using (Html.BeginForm())
                        {
                            <div id="div_@Count" style="display: none;">
                                @{CommentParentID = comment.Id;
                                  Model.AddNewComment.CommentParentID = CommentParentID;
                                  Model.AddNewComment.CommentText = comment.CommentText;}
                                <div id="ChildComments">
                                    @Html.HiddenFor(model => model.AddNewComment.CommentParentID)
                                    @Html.Label("Reply Comment")
                                    <div>
                                        @Html.TextAreaFor(model => model.AddNewComment.ChildCommentText)
                                    </div>
                                    @Html.HiddenFor(model => model.AddNewComment.CommentText)
                                </div>
                                <div class="buttons">
                                    <input type="submit" name="reply-comment"  value="@T("Blog.Comments.ReplyButton")" />
                                </div>
                            </div>



                        }