有没有办法将页面作为对话框加载?

时间:2013-04-09 15:17:11

标签: javascript jquery asp.net-mvc dialog jquery-ui-dialog

我正在使用ASP.NET MVC 3.每次向用户展示View 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>

以下是我打开对话框的方法 onclick =“openDialog('Nova rede','Redes / Novo')”。当View第一次打开时,它工作正常,但是当它返回HttpPost时,页面不能作为对话框打开。我试图将View Novo的内容放入div中并使用函数(document).ready来调用JQuery函数对话框以始终将其作为对话框打开,但不起作用。

function 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;
        }  

Novo View

@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>

有没有办法每次打开此视图时,它会自动打开为对话框? 我在网上找到的东西没有帮助,有什么建议吗?

0 个答案:

没有答案