【问题标题】:How to render a partial view on click in link如何在点击链接时呈现部分视图
【发布时间】:2017-03-20 19:46:29
【问题描述】:

我有一个包含信息的表格,我想单击链接(这是一个引导 gliphycon),当我单击此链接时,我想在引导模式中呈现一个包含有关此列表项的所有信息的部分视图。

我的控制器

public PartialViewResult Details(int id)
        {
            try
            {
                return PartialView("Details", _paisRepository.GetDetails(id));
            }
            catch (Exception)
            {
                ViewBag.error = "Não foi possivel buscar os dados!";
                return PartialView("Error");
            }
        }

我以这种方式调用将数据返回到模态的操作方法:

<a href="/Pais/Details/@item.ID">
   <div class="glyphicon glyphicon-search" style="cursor:pointer"></div>
</a>

和引导模式

<div class="modal" id="exibirPais" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">
                    <span class="glyphicon glyphicon-search"></span>
                    <label class="control-label">Exibir</label>
                </h4>
            </div>
            <div class="modal-body">

                @Html.Partial()

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger">Fechar</button>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

标签: asp.net-mvc twitter-bootstrap-3


【解决方案1】:

第一:

为您的 glyphicon 添加一个 javascript 函数调用并发送当前对象项 ID

   <span class="glyphicon glyphicon-search" style="cursor:pointer" data- onclick="ShowDetails('@item.id')"></span>

将您的模态更改为以下内容:

<div class="modal" id="exibirPais" data-backdrop="static">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">
                <span class="glyphicon glyphicon-search"></span>
                <label class="control-label">Exibir</label>
            </h4>
        </div>
        <div class="modal-body" id="Body">
         @*  Body where the  partial view will be rendered using AJAX*@

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-danger">Fechar</button>
        </div>
    </div>
</div>

还有你的javascript函数:

 function ShowDetails(Id) {
        $("#exibirPais").modal();
        $.ajax({
            url: '/Controller/Details/' + Id,
            success: function(result) {
                $('#Body').html(result);

            }
        });
    }

【讨论】:

    【解决方案2】:

    你应该监听这个 a 标签上的点击事件,并使用 ajax 向服务器操作方法发出请求,并使用返回的响应来构建模态对话框。

    所以在你的主视图中,当你渲染a标签时,你可以将href值设置为action方法的url路径。

    <table>
    @foreach (var item in SomeCollection)
    {
      <tr>
         <td>
             <a href="@Url.Action("Details",new {id=item.Id})" class="modal-link">
                         <div class="glyphicon glyphicon-search" style="cursor:pointer"></div>
             </a>
         </td>
      </tr>
    }
    </table>
    

    我们向锚标签添加了一个 css 类 modal-link。现在让我们在锚标签上添加监听点击事件(使用这个 css 类),对点击链接的 href 值进行 tan ajax 调用。当 ajax 方法返回结果(action 方法生成的 html 标记)时,我们将其作为模态对话框的内容。

    $(function() {    
    
         $('body').on('click', '.modal-link',function (e) {
             e.preventDefault();
    
             $("#myModal").remove();
    
             $.get($(this).attr("href"),function (data) {
                   $('<div id="myModal" class="modal fade"><div class="modal-dialog" 
                                                                        role="document">' +
                                data +'</div></div>').modal();
             });
         });
    
    })
    

    此代码将使用来自Details 操作方法的部分视图结果替换模态框的全部内容。这意味着you need to include the necessary markup needed for the modal header and footer(带有按钮)在Details.cshtml 部分视图中。

    【讨论】:

      【解决方案3】:

      通过javascript中的点击事件

       $("#exibirPais").load('@Url.Action("PartialViewResult", "Controller_Name")');
      

      注意:PartialViewResult 必须返回 PartialView(model); 而.cshtml页面是部分页面

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-12
        • 2020-02-14
        • 2019-12-14
        • 2013-08-16
        • 1970-01-01
        • 1970-01-01
        • 2015-05-22
        相关资源
        最近更新 更多