简体   繁体   English

在asp.net MVC操作的弹出窗口中打开视图,单击

[英]Open the view in popup in asp.net MVC action click

I am working on small POC of asp.net mvc scaffolding. 我正在研究asp.net mvc脚手架的小型POC。 I have one action link which calls another controller's action method which in turn opens up the view of that action method. 我有一个操作链接,该链接调用另一个控制器的操作方法,该操作方法又打开了该操作方法的视图。 But i want that view to be open in popup not in new tab. 但是我希望该视图在弹出窗口中打开,而不是在新选项卡中打开。 Following is the code of action link 以下是动作代码链接

@Html.ActionLink("View Clients", "Details", "Client", new { id=item.Id})

In above code 'View Clients' is the link name which calls 'Details' method of 'Client controller' which passes ID as parameter as well. 在上面的代码中,“查看客户端”是调用“客户端控制器”的“详细信息”方法的链接名称,该方法也将ID作为参数传递。 Following is the Client controller: 以下是客户端控制器:

 public class ClientController : Controller
{
  public ActionResult Details(long id = 0)
    {
        Client client = db.Clients.Find(id);
        if (client == null)
        {
            return HttpNotFound();
        }
        return View(client);
    }
  }

Above controller method returns the details view of Client controller. 上面的控制器方法返回Client控制器的详细信息视图。

So what i want to do here is to open that particular view in the popup. 所以我在这里要做的就是在弹出窗口中打开该特定视图。 Does anybody have solution on this ? 有人对此有解决方案吗?

Use an ajax call to return the partial view and add it to the DOM. 使用ajax调用返回部分视图并将其添加到DOM。

var url = '@Url.Action("Details", "Client")';

$('selector').load(url, { id: 1 });

Add custom attribute to the link, use its URL to load the PartialView from controller in the modal form. 自定义属性添加到链接,使用其URL以模态形式从控制器加载PartialView

View: 视图:

@Html.ActionLink("View Clients", "Details", "Client", new { id = item.Id }, new { data_modal = "" })

<!-- modal placeholder-->
<div id='myModal' class='modal fade in'>
    <div class="modal-dialog">
        <div class="modal-content">
            <div id='myModalContent'></div>
        </div>
    </div>
</div>

Javascript: 使用Javascript:

$(function () {
    $.ajaxSetup({ cache: false });
    $("a[data-modal]").on("click", function (e) {        
        $('#myModalContent').load(this.href, function () {
            $('#myModal').modal({
                keyboard: true
            }, 'show');
        });
        return false;
    });
});

Controller: 控制器:

public class ClientController : Controller
{
  public ActionResult Details(long id = 0)
    {
        Client client = db.Clients.Find(id);
        if (client == null)
        {
            return HttpNotFound();
        }
        return PartialView(client);
    }
}

Reference: http://www.advancesharp.com/blog/1125/search-sort-paging-insert-update-and-delete-with-asp-net-mvc-and-bootstrap-modal-popup-part-1 http://www.advancesharp.com/blog/1126/search-sort-paging-insert-update-and-delete-with-asp-net-mvc-and-bootstrap-modal-popup-part-2 参考: http : //www.advancesharp.com/blog/1125/search-sort-paging-insert-update-and-delete-with-asp-net-mvc-and-bootstrap-modal-popup-part-1 http ://www.advancesharp.com/blog/1126/search-sort-paging-insert-update-and-delete-with-asp-net-mvc-and-bootstrap-modal-popup-part-2

Note: ASP MVC will automatically convert underscores in html attribute properties to dashes. 注意:ASP MVC会将html属性属性中的下划线自动转换为破折号。 https://stackoverflow.com/a/4515095/3387187 https://stackoverflow.com/a/4515095/3387187

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM