我正在使用 ASP.NET MVC 3。每次我向用户展示 View Novo 时,我都需要将此 View 作为对话框打开。
控制器
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')" 的方法。第一次打开视图时它工作正常,但是当它返回 HttpPost 时,页面不会作为对话框打开。我试图将View Novo的内容放入一个div中,并使用函数(document).ready调用JQuery函数对话框来始终将其作为对话框打开,但没有奏效。
函数打开对话框
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;
}
新视野
@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>
有没有办法每次我打开这个视图时,它会自动作为对话框打开?我在网上找到的没有帮助,请有什么建议吗?