Javascript 是否有一种方法可以始终将页面作为对话框加载?

Javascript 是否有一种方法可以始终将页面作为对话框加载?,javascript,jquery,asp.net-mvc,dialog,jquery-ui-dialog,Javascript,Jquery,Asp.net Mvc,Dialog,Jquery Ui Dialog,我正在使用ASP.NETMVC3。每次我向用户呈现视图Novo时,我都需要将此视图作为对话框打开 控制器 public ActionResult Index() { IndexViewModel viewModel = new IndexViewModel(); viewModel.listaRedes = new SwitchBLL().GetRedes(); return View(viewModel

我正在使用ASP.NETMVC3。每次我向用户呈现视图Novo时,我都需要将此视图作为对话框打开

控制器

 public ActionResult Index()
        {
            IndexViewModel viewModel = new IndexViewModel(); 
            viewModel.listaRedes = new SwitchBLL().GetRedes();
            return View(viewModel);
        }

        public ActionResult Novo()
        {
            return View(new Redes());
        }

        [HttpPost]
        public ActionResult Novo(Redes model)
        {
            if (ModelState.IsValid)
            {
                //Do something
                ModelState.AddModelError("", "Cadastrado com sucesso!");
            }
            else
            {
                ModelState.AddModelError("", "Não foi possível cadastrar. Por favor, tente novamente!");
            }
            return View(model);
        }
索引视图

@model CW.ViewModel.Redes.IndexViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<button class="btnNovo" onclick="openDialog('Nova rede','Redes/Novo')">Novo</button>
<table class="listas" id="rowSwitches" style="width: 100%;">
    @foreach (var rede in Model.listaRedes)
    {<tr onclick="jqGetRede(@rede.rede_id)" title="Detalhes do Servidor @rede.rede_u_name" >
        <td>
            <img width="12" src="/Content/Master/edit-icon.png" alt="Editar" />
        </td>
        <td style="text-align: left;">@rede.rede_u_name
        </td>
    </tr>}
</table>
@model CW.ViewModel.Redes.IndexViewModel
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
诺沃
@foreach(Model.listaRedes中的var rede)
{
@rede.rede___名称
}
下面是我打开对话框的方式:onclick=“openDialog('Nova rede','Redes/Novo')”。当视图第一次打开时,它可以正常工作,但是当返回HttpPost时,页面不会作为对话框打开。我尝试将ViewNovo的内容放入一个div中,并使用函数(document)。准备好调用JQuery函数对话框以始终将其作为对话框打开,但没有成功

函数openDialog

function openDialog(title, url) {
            $('.opened-dialogs').dialog("close");

            $('<div class="opened-dialogs">').html('loading...').dialog({
                position: ['center', 20],
                resizable: true,
                draggable: true,
                open: function () {
                    $(this).load(url);

                },
                close: function (event, ui) {
                    $(this).remove();
                },

                title: title,
                minWidth: 600
            });
            return false;
        }  
函数openDialog(标题、url){
$('.opened dialogs').dialog(“close”);
$('').html('加载…')。对话框({
位置:[‘中间’,20],
可调整大小:正确,
真的,
打开:函数(){
$(this.load)(url);
},
关闭:功能(事件、用户界面){
$(this.remove();
},
标题:标题,,
最小宽度:600
});
返回false;
}  
诺沃视图

@model CW.Models.Redes.Redes
@{
    Layout = null;
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "")
    <div id="addNew">
        <fieldset>
            <legend>Novo</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.NomeServidor)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.NomeServidor)
                @Html.ValidationMessageFor(m => m.NomeServidor)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.TipoServidor)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.TipoServidor)
                @Html.ValidationMessageFor(m => m.TipoServidor)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Descricao)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Descricao)
                @Html.ValidationMessageFor(m => m.Descricao)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.ServidorURL)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.ServidorURL)
                @Html.ValidationMessageFor(m => m.ServidorURL)
            </div>
            <p>
                <input type="submit" value="save" />
            </p>
        </fieldset>
    </div>
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">
</script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@model CW.Models.Redes.Redes
@{
布局=空;
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true,“”)
诺沃
@LabelFor(m=>m.NomeServidor)
@Html.TextBoxFor(m=>m.NomeServidor)
@Html.ValidationMessageFor(m=>m.NomeServidor)
@LabelFor(m=>m.TipoServidor)
@Html.TextBoxFor(m=>m.TipoServidor)
@Html.ValidationMessageFor(m=>m.TipoServidor)
@LabelFor(m=>m.descripcao)
@Html.TextBoxFor(m=>m.descripcao)
@Html.ValidationMessageFor(m=>m.descripcao)
@LabelFor(m=>m.ServidorURL)
@Html.TextBoxFor(m=>m.ServidorURL)
@Html.ValidationMessageFor(m=>m.ServidorURL)

}
有没有办法在每次打开此视图时自动将其作为对话框打开?
我在网上找到的东西没有帮助,请给我任何建议?

这并不像你想的那么简单。在
[Post]Novo
操作中,必须返回
索引
视图,因为这是呈现对话框的视图。然后必须修改
IndexViewModel
,使其具有类型为
Redes
的属性。最后,在
索引
视图上,您必须测试
Model.Redes
是否为空,并使用相应的数据(如验证消息)打开对话框。@AndreCalil我需要从其他视图调用该视图,而不仅仅是索引。如果我在HttpPost中返回索引,它将始终返回索引视图,对吗?如果需要从索引以外的其他视图调用它,会发生什么?谢谢您可以将返回视图作为参数传递,但事情会变得棘手。在这种情况下,最简单的方法是更改
[Post]Novo
操作以返回
JsonResult
并在每个调用页面上使用javascript。感谢您的帮助!不。你需要帮忙吗?