2017-02-14 55 views
0

佈局 - >查看 - >管窺ASP.NET MVC是否可以在局部視圖中使用ajax.beginform?

在查看:

 <div class="col-md-8"> 
      @{Html.RenderPartial("_komentari", commentlist);} 
      <div class="gap gap-small"></div> 
      <div class="box bg-gray"> 
       <h3>@Res.commentwrite_title</h3> 
       @using (Ajax.BeginForm("postcomment", new { propertyid = Model.PublicID }, new AjaxOptions { UpdateTargetId = "commentsarea", HttpMethod = "Post", InsertionMode = InsertionMode.Replace }, null)) 
       { 
        <div class="row"> 
         <div class="col-md-8"> 
          <div class="form-group"> 
           <label>@Res.commentwrite_content</label> 
           <textarea id="comment" name="comment" class="form-control" rows="6"></textarea> 
          </div> 
          <div class="form-group"> 
           <input class="btn btn-primary" type="submit" value='@Res.commentwrite_btn' /> 
          </div> 
         </div> 
        </div> 
       } 
      </div> 
     </div> 

在局部視圖:

<div id="commentsarea"> 
    <ul class="booking-item-reviews list"> 
     @if (Model != null) 
     { 
      foreach (Comment item in Model) 
      { 
       <li> 
        <div class="row"> 
         <div class="col-md-2"> 
          <div class="booking-item-review-person"> 
           <a class="booking-item-review-person-avatar round" href="#"> 
            <img src="/assets/img/70x70.png" alt="Image Alternative text" title="Bubbles" /> 
           </a> 
           <p class="booking-item-review-person-name"> 

            @if (item.UserId != null) 
            { 
             <a href='@Url.Action("details", "user", new { userid = item.User.PublicId })'>@item.User.Username</a> 
            } 
            else 
            { 
             <a href="#">Anonymous</a> 
            } 

           </p> 
          </div> 
         </div> 
         <div class="col-md-10"> 
          <div class="booking-item-review-content"> 
           <p> 
            @item.Content 
           </p> 
           <p class="text-small mt20">@item.DateOnMarket</p> 

           <p class="booking-item-review-rate"> 
@using (Ajax.BeginForm("reportcomment", new { comment = item.PublicId }, new AjaxOptions { UpdateTargetId = "reportscount", HttpMethod = "Post", InsertionMode = InsertionMode.Replace }, null)) 
        { 
            <a id="submit_link" href="#">Spam?</a> 
            <a class="fa fa-thumbs-o-down box-icon-inline round" href="#"></a> 
        } 
    <b id="reportscount" class="text-color">@item.CommentReports.Count</b> 
           </p> 
          </div> 
         </div> 
        </div> 
       </li> 
      } 

     } 
    </ul> 
</div> 

<script> 
    $(function() { 
     $('a#submit_link').click(function() { 
      $(this).closest("form").submit(); 
     }); 
    }); 
</script> 

查看

兩個腳本阿賈克斯總是包含在佈局中(我已經在使用Ajax也在其他頁面上)。在View頁面上,當我添加一條新評論時,它會添加到數據庫中,並通過ajax顯示更新的評論列表。一切正常。

管窺

但是,如果我想報告爲垃圾郵件的評論,我必須單擊局部視圖(#submit_link)內部鏈接和報告之後,#reportscount部分裏面,我想顯示該評論的更新報告數量。 actionresults返回像Content(numberofreports.toString())那樣的數字。它的工作原理,但我在空白頁中獲得號碼?

非常感謝。

回答

0

如果你不這樣做會更好。主要問題是,出於安全原因,當HTML被插入到DOM中時,<script>標籤被忽略。由於Ajax.*助手系列通過插入<script>標籤來工作,所以當您通過AJAX返回部分內容時,這些標籤就不會出現。如果您只在表單中包含表單的HTML內容,而不是表單本身,則更好。

+0

噢,謝謝!所以,我應該使用佈局 - 視圖 - 部分視圖 - 部分視圖?那可能嗎? – user5618385

相關問題