[英]javascript/jquery modal popup dialog MVC 4 / render partial view
I have been using the DevExpress PopupControl. 我一直在使用DevExpress PopupControl。 They look nice and pretty but they do not display the scrollbars on iOS/Android devices.
它们看起来很漂亮,但它们不会在iOS / Android设备上显示滚动条。 So I want to come up with an alternative.
所以我想提出一个替代方案。 My immediate use is just for displaying a partial view, read only and a close button.
我的直接用途仅用于显示部分视图,只读和关闭按钮。
I am not familiar with jquery so I am having a hard time piecing together all the different posts about this topic. 我对jquery不熟悉,所以我很难拼凑关于这个主题的所有不同帖子。
My index.cshtml is a portal with many different partial views. 我的index.cshtml是一个包含许多不同局部视图的门户。 One of the partial views is a list of clients.
部分视图之一是客户列表。 The client name is a link to client detail.
客户端名称是客户端详细信息的链接。 This is where I need the popup dialog.
这是我需要弹出对话框的地方。
Partial view with client list (note the link calls a javascript function passing the ID I want to view: 部分视图与客户端列表(注意链接调用javascript函数传递我想要查看的ID:
<table style="text-align: left;">
@if ((Model != null) && (Model.Items != null))
{
foreach (WebMVC.Models.VisitDetails p in Model.Items)
{
sTime = p.StartTime.ToString("MM/dd") + " " + p.StartTime.ToShortTimeString().PadLeft(8,'_') + " - " + p.EndTime.ToShortTimeString().PadLeft(8,'_');
<tr>
<td style="width: auto">
@Html.DevExpress().HyperLink(
settings =>
{
settings.Name = "indexHyperLinkClient" + p.VisitID.ToString();
settings.Properties.Text = @p.NameNumZone;
settings.Properties.ClientSideEvents.Click =
string.Format("function(s, e) {{ MethodClient('{0}'); }}", p.Account);
}
).GetHtml()
</td>
</tr>
}
}
</table>
current javascript in index.cshtml that handles the popup: 处理弹出窗口的index.cshtml中的当前javascript:
<script type="text/javascript">
var _clientId;
function MethodClient(clientid) {
_clientId = clientid;
popClient.PerformCallback();
popClient.Show();
}
function OnBeginCallbackClient(s, e) {
e.customArgs["clientid"] = _clientId;
}
<script type="text/javascript">
popClient is the current dialog that I want to replace. popClient是我想要替换的当前对话框。 I would like the dialog to be a specific height regardless of the content size.
无论内容大小如何,我都希望对话框具有特定的高度。
example of the partial view to be displayed in the dialog: 要在对话框中显示的局部视图的示例:
@model WebMVC.Models.ClientDetail
@{
DateTime now = DateTime.Today;
int age = now.Year - Model.Birthdate.Year;
if (Model.Birthdate > now.AddYears(-age))
{
age--;
}
string sBirthdate = Model.Birthdate.ToShortDateString() + " (Age: " + age + ")";
}
<div id="contentDiv">
<span class="display-label">@Html.DisplayNameFor(model => model.NameNumZone):</span>
<span class="display-field">@Html.DisplayFor(model => model.NameNumZone)</span>
<br />
<span class="display-label">@Html.DisplayNameFor(model => model.Sex):</span>
<span class="display-field">@Html.DisplayFor(model => model.Sex)</span>
<br />
<span class="display-label">@Html.DisplayNameFor(model => model.Birthdate):</span>
<span class="display-field">@Html.DisplayFor(model => @sBirthdate)</span>
<br />
<span class="display-label">@Html.DisplayNameFor(model => model.Address):</span>
<span class="display-field">@Html.DisplayFor(model => model.Address)</span>
<br />
</div>
Controller: 控制器:
public ActionResult Details()
{
string id = "";
if (!string.IsNullOrEmpty(Request.Params["clientid"]))
id = Request.Params["clientid"];
int clientid = 0;
if (id != "")
clientid = Convert.ToInt32(id);
ClientDetail cl;
if (clientid != 0)
ClientDetail cl = GetClientDetails(clientid);
else
ClientDetail cl = new ClientDetail();
return PartialView("ClientPopupPartial", cl);
}
Can I have one popup and render different partial views (maybe by adding a hardcoded param such as area = 1, area = 2 to the method client call)? 我可以有一个弹出窗口并渲染不同的局部视图(可能通过向方法客户端调用添加一个硬编码的参数,例如area = 1,area = 2)? Or should there be one popup for each area of detail (client, visit, directions...).
或者每个细节区域都应该有一个弹出窗口(客户端,访问,方向......)。
Example with a static dialog (No AJAX) 带静态对话框的示例(无AJAX)
Define a div
for your dialog content in a partial view: 在局部视图中为对话框内容定义
div
:
@model ClientDetail
<h2>Client Detail</h2>
@Html.DisplayFor(m => m.NameNumZone)
@Html.DisplayFor(m => m.Birthdate)
...
Dialog trigger and partial view: 对话框触发器和局部视图:
<a href="#" class="dialog-trigger" data-clientId="@p.Account">@p.NameNumZone</a>
<div id="client-detail-modal">
@Html.Partial("ClientDetail", Model.Item)
</div>
Javascript: 使用Javascript:
$(document).ready(function() {
// setup the dialog
$("#client-detail-modal").dialog({
modal: true,
autoOpen: false,
height: 100,
width: 200
});
// bind the click event
$(".dialog-trigger").on("click", function(event) {
event.preventDefault();
$("#client-detail-modal").dialog("open"); // show dialog
});
});
Now if you have more than one client on a page you'll need a dialog per client. 现在,如果页面上有多个客户端,则每个客户端都需要一个对话框。 After a few clients it gets ugly.
在几个客户之后,它变得丑陋。 Instead, fill the dialog content dynamically.
而是动态填充对话框内容。
Dynamic dialog content (AJAX) 动态对话框内容(AJAX)
Dialog container for your partial is empty initially: 您的partial的对话框容器最初为空:
<div id="client-detail-modal"><!-- Client Partial, empty for now --></div>
Get the partial via AJAX: 通过AJAX获取部分:
$(".dialog-trigger").on("click", function(event) {
event.preventDefault();
var clientId = $(this).data("clientId");
$.ajax({
url: "Client/Details/" + clientId,
type: "GET",
})
.done(function(result) {
$("#client-detail-modal").html(result).dialog("open");
});
});
Dynamic content (No AJAX) 动态内容(无AJAX)
Another way to fill the dialog would be to populate the data
attributes of the trigger element then replace content using javascript. 填充对话框的另一种方法是填充触发元素的
data
属性,然后使用javascript替换内容。
<a href="#" class="dialog-trigger"
data-clientId="@p.Account"
data-birthdate="@p.Birthdate">@p.NameNumZone</a>
$(".dialog-trigger").on("click", function(event) {
var clientId = $(this).data("clientId");
var birthdate = $(this).data("birthdate");
// now replace content with new values
$("span.birthdate").text(birthdate);
});
Put this content in your style sheet 将此内容放在样式表中
.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalDialog:target {
opacity:1;
pointer-events: auto;
}
.modalDialog > div {
width: 80%;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}
.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}
.close:hover { background: #00d9ff; }
and in the code use the following 并在代码中使用以下内容
<a href="#openModal" target="">Open Modal</a>
<div id="openModal" class="modalDialog" data-theme="c">
<div>
<a href="#close" title="Close" class="close" target="">X</a>
<h2>Pop up</h2>
<p>Pop up content. You can add your controls and content here</p>
</div>
</div>
this logic worked for me. 这个逻辑对我有用。 Hope it works for you also.
希望它也适合你。
Instead of using <a href="#close" title="Close" class="close" target="">X</a>
for closing it is preferred you navigate to some parent page instead. 而不是使用
<a href="#close" title="Close" class="close" target="">X</a>
进行关闭,而是首选导航到某个父页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.