How to open a partial view by using jquery modal popup in asp.net MVC?

给你一囗甜甜゛ 提交于 2019-12-03 17:13:56

I have this working well in my current project.

A partial view uses the Inherits control markup just like a full view does to strongly type the Model object to a datatype.

Here is a very simple example of a partial view that is returned via a ajax call and put inside a div. The purpose of this partialview is to display a text message that is passed through to it.

LiteralMessage.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<!-- LiteralMessage.ascx start -->
<%= Model %>
<!-- LiteralMessage.ascx end -->

Controller Method

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReturnId(int id)
{
    return PartialView("LiteralMessage", string.Format("Hello world! Id: {0}", id));
}

Note that in the partial page view can be any complex object.

I hope this helps!

EDIT: and because this is listed as jQuery as well, use this as your ajax's success event. (This is assuming your dialog has a content DIV with an Id of MyDialogMessage inside a dialog DIV with an Id of MyDialog)

// executes when $.post is complete
function doSuccess(result)
{
    $('div#MyDialog div#MyDialogMessage').html(result);
    //show dialog
    $('div#MyDialog').dialog('open');
}

I would create a action on a conroller that returns the partial view. Then use Colorbox (via its IFRAME attribute) to load the results of the controller via jQuery.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!