在特定标记后插入局部视图

时间:2012-02-01 07:52:45

标签: jquery asp.net-mvc

我有以下标记:

<ul>
  <li class="comment">
    <p>comment 1</p>
    <div class="info">
      <img src="/assets/images/i/2.jpg" width="17px" height="15px">
      <a>sharok</a>
      <span>·</span>
      <span>21.12.2012 0:00:00</span>
      <span>·</span>
      <a href="#">
                    Reply</a>
      <span>·</span>
      <a href="#">Complain</a>
    </div>
    <ul> ***this is child comment
      <li class="comment">
        <ul>
          <li class="comment">
            <p>comment 1.1</p>
            <div class="info">
              <img src="/assets/images/i/2.jpg" width="17px" height="15px">
              <a>sharok</a>
              <span>·</span>
              <span>25.12.2012 0:00:00</span>
              <span>·</span>
              <a href="#">
                    Reply</a>
              <span>·</span>
              <a href="#">Complain</a>
            </div>
            <ul>
              <li class="comment">

             </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

我希望用户点击Reply链接,然后在div class="info"之后显示我的部分视图。

这是我的部分观点:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DT.KazBilet.Objects.PublicationComment>" %>
<div class="wrap">
    <h4>Comment</h4>
    <%using (Ajax.BeginForm("DoComment", "Publication", new {id = Model.Publication.OID},new AjaxOptions()))
      {%>
    <%if(Model.ParentCommentId != null) %>
    <%=Html.Hidden("ParentCommentId", Model.ParentCommentId) %>
    <%=Html.TextAreaFor(x=>x.Text) %>        
    <input type="submit" value="Publish" class="btn ok_btn" />
    <%}%>
</div>

我该怎么做?

1 个答案:

答案 0 :(得分:2)

这可以使用$.get函数(http://api.jquery.com/jQuery.get/)使用ajax完成。但是,您应该考虑直接在HTML中输入您的注释div并将其隐藏,直到用户单击“回复”按钮,然后显示它(滑动,淡入等),而不是从服务器获取它。由于每次都是相同的内容,因此一旦用户想要发布回复就没有动态获取内容。

如果您仍想动态获取它,请执行以下操作:

$(".replybtn").click(function() {
    $.get('path/to/partial/view', function(data) {
        $(this).parent().after(data);
    });
});

您应该在回复链接/按钮中添加一个类,否则很难捕获它上面的click事件。