我使用AJAX来完成这个操作。您有一个部分,其中包含典型的Twitter模态框模板HTML代码:
<div class="container">
<div class="modal fade" id="LocationNumberModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title">
Serial Numbers
</h4>
</div>
<div class="modal-body">
<span id="test"></span>
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
</div>
接下来是你的控制器方法,我使用JSON并有一个自定义类将视图呈现为字符串。我这样做是为了在一个Ajax调用中执行多个屏幕上的Ajax更新。参考链接:示例,但如果您只进行一次调用,则可以在返回时使用PartialViewResult/ActionResult。我将使用JSON来展示它。
控制器中的JSON方法:
public JsonResult LocationNumberModal(string partNumber = "")
{
return Json(new {
LocationModal = ViewUtility.RenderRazorViewToString(this.ControllerContext, "LocationNumberModal.cshtml", new SomeModelObject())
},
JsonRequestBehavior.AllowGet
);
}
然后,在使用您的模态框时:您可以将AJAX打包在您的部分视图中,并调用@{Html.RenderPartial... 或者您可以使用带有div的占位符:
<div id="LocationNumberModalContainer"></div>
然后是你的ajax请求:
function LocationNumberModal() {
var partNumber = "1234";
var src = '@Url.Action("LocationNumberModal", "Home", new { area = "Part" })'
+ '?partNumber='' + partNumber;
$.ajax({
type: "GET",
url: src,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
$("#LocationNumberModalContainer").html(data.LocationModal);
$('#LocationNumberModal').modal('show');
}
});
};
那么您的模态框按钮是这样的:
<button type="button" id="GetLocBtn" class="btn btn-default" onclick="LocationNumberModal()">Get</button>