2013-01-04 96 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提交表單或發佈信息,則無法將字段值從頁面傳遞到控制器。 –