Asp.net mvc 3 MVC-Jquery用户界面-对话框-问题
我试图在MVC中创建一个对话框窗口,而不使用AjaxActionLink。在本例中,我使用jQueryUI1.8.13Asp.net mvc 3 MVC-Jquery用户界面-对话框-问题,asp.net-mvc-3,jquery-ui-dialog,Asp.net Mvc 3,Jquery Ui Dialog,我试图在MVC中创建一个对话框窗口,而不使用AjaxActionLink。在本例中,我使用jQueryUI1.8.13 请考虑以下片段: public class ModalViewModel { public string Title { get; set; } public string Description { get; set; } } 以及控制器: private ActionResult GetVisitors(VisitorSearchViewModel mod
请考虑以下片段:
public class ModalViewModel
{
public string Title { get; set; }
public string Description { get; set; }
}
以及控制器:
private ActionResult GetVisitors(VisitorSearchViewModel model)
{
VisitorSearchResponse response =
.... Omitted for brevity
if (response.Success)
{
return View("VisitorList", response.Visitors.ToList());
}
else
{
return RedirectToAction("Error");
}
}
public PartialViewResult Error()
{
return PartialView("Modal",
new ModalViewModel()
{
Title = "Test Title",
Description = "Test Description"
});
}
最后,模态共享视图:
@model CraftTraxNG.Model.ViewModels.ModalViewModel
<script type="text/javascript">
$(document).ready(function () {
$("#errorMessage").dialog(
{ autoOpen: true,
title: '@Model.Title',
width: 500,
height: 100,
modal: true,
buttons:{ "OK": function () {
$(this).dialog("close"); }
}
});
});
当遇到错误时,它将写出部分视图内容;即,它成功地创建了具有适当样式的对话框窗口。我的问题是我想在我当前所在的视图上渲染部分视图。我以前从未用过这种方式。现在,当它呈现部分视图时,我将丢失当前所在视图的内容
通常,我可以使用AjaxActionLink,只需设置一个我希望对话框呈现的div标记,然后替换它或在之后、之前等插入它
在本例中,我将前往服务并获取服务器端响应,如果响应失败,我需要一种方法将常规视图上的一些div标记替换为该部分视图
感谢您的帮助
谢谢。我遇到了这个问题,还没有找到理想的解决方案。这是我通常做的事
<a href="#" id="loadsContentOrError">Click Me</a>
$(function() {
$("#loadsContentOrError").click(e){
e.preventDefault();
$.ajax({
url: '<%: Url.Action("Error") %>',
success: function(html) {
html = $(html);
if(html.find('.errorMessage').length > 0) {
$('#divToLoadContentTo").append(html);
} else {
$('#divToLoadContentTo").html(html);
}
}
});
});
});
这很粗糙,但它给了你一个很好的起点
<a href="#" id="loadsContentOrError">Click Me</a>
$(function() {
$("#loadsContentOrError").click(e){
e.preventDefault();
$.ajax({
url: '<%: Url.Action("Error") %>',
success: function(html) {
html = $(html);
if(html.find('.errorMessage').length > 0) {
$('#divToLoadContentTo").append(html);
} else {
$('#divToLoadContentTo").html(html);
}
}
});
});
});