2013-01-04 57 views
0

@foreach(在Model.Comments.Where VAR commentlist(X => x.CommentParentID == 0)){ @ Html.Hidden get和设定值剃刀MVC 4

    <div class="blog-comment"> 
         <div class="comment-info"> 
          <div class="user-info"> 
           @if (commentlist.AllowViewingProfiles) 
           { 
            <a href="@Url.RouteUrl("CustomerProfile", new { id = commentlist.CustomerId })" class="username">@(commentlist.CustomerName)</a> 
           } 
           else 
           { 
            <span class="username">@(commentlist.CustomerName)</span> 
           } 
           <div class="avatar"> 
            @if (!String.IsNullOrEmpty(commentlist.CustomerAvatarUrl)) 
            { 
             <img src="@(commentlist.CustomerAvatarUrl)" class="avatar-img" title="avatar" alt="avatar" /> 
            } 
           </div> 
          </div> 
         </div> 
         <div class="comment-content"> 
          <div class="comment-time"> 
           @T("Blog.Comments.CreatedOn"): <span class="stat-value">@commentlist.CreatedOn.ToString("g")</span> 
           <div class="buttons"> 
            <input type="submit" id="reply" class="button-1 blog-post-add-comment-button" onclick="return showHide();" /> 
            @Html.Hidden("CommentParentID",@commentlist.Id) 
           </div> 
          </div> 
          <div class="comment-body"> 
           @Html.Raw(Nop.Core.Html.HtmlHelper.FormatText(commentlist.CommentText, false, true, false, false, false, false)) 
          </div> 
         </div> 

         <div class="clear"> 
         </div> 
} 

我使用@ Html.Hidden(“CommentParentID”,@ commentlist.Id)为ChildComment设置值CommentParentID(如果有的话)。

在下面的操作我想通过CommentParentID作为参数。

@Html.ActionLink("Reply", "BlogCommentReply", "Blog", new { blogPostId = blogPostId, CommentParentID=CommentParentID,captchaValid = Model.AddNewComment.DisplayCaptcha }, null) 

我如何检索控制器中隐藏的字段值? 或我如何传递这个值?

回答

1

我如何检索控制器中的隐藏字段值?

通过让控制器动作的参数具有相同的名称作为您的隐藏字段:在这里我使用了一个集合,因为从我所看到的你把那些隐藏字段内

[HttpPost] 
public ActionResult SomeAction(string[] commentParentID) 
{ 

} 

公告一个循环,这意味着你将有多个隐藏的元素具有相同的名称POST到服务器。

您也可以使用这些Ids是整数的整数数组。

0

最简单的方法是通过Ajax像我不使用AJAX的任何地方这

控制器

public void Method1(string val1, string val2) 
     { 
      ///do what you want with the values 
     } 

页/ Ajax调用

function PostData() { 
      $.ajax({ 
       url: 'Home/Method1', 
       data: { val1: $('#hidden1').val(), val2: $('#hidden2').val()}, 
       success: function (data) { 
       } 
      }); 
     } 
+0

。 – NetraSW

+0

表格怎么样?看起来你的HTML页面没有形式。请注意,如果不使用Ajax提交表单或发布信息,则无法将字段值从页面传递到控制器。 –