如果要在模型弹出窗口中打开 EditPartial Action 方法的结果,则需要一些模型弹出代码。
jQuery UI 是一种选择。http://jqueryui.com/demos/dialog/
1)在您的页面中包含 jQuery UI 参考,
2)将以下脚本添加到您的页面,这会将您的正常链接转换为模型弹出窗口
<script type="text/javascript">
$(function(){
$(".actionLink").click(function (e) {
var url = this.href;
var dialog = $("#dialog");
if ($("#dialog").length == 0) {
dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
}
dialog.load(
url,
{}, // omit this param object to issue a GET request instead a POST request, otherwise you may provide post parameters within the object
function (responseText, textStatus, XMLHttpRequest) {
dialog.dialog({
close: function (event, ui) {
dialog.remove();
},
modal: true,
width: 460, resizable: false
});
}
);
return false;
});
});
</script>
从您的操作结果中,您可以返回要在模型弹出窗口中显示的任何标记。大多数情况下,您将返回一个视图。如果要显示部分视图,如果是 ajax 请求,如果是正常请求则显示正常视图,您可以检查 Request.IsAjaxRequest 方法来做到这一点。
public ActionResult EditPartial(int id)
{
CustomerViewModel objCustomer=GetCustomer(id);
if(Request.IsAjaxRequest())
{
return View("Partial/Edit",objCustomer);
}
return View(objCustomer);
}
假设您有 2 个视图来显示您的正常页面和部分页面(用于模型弹出窗口)
我更喜欢将我的操作方法命名为,Edit
而不是EditPartial
,因为它同时处理请求(ajax 和普通)