MVC C# 模态弹出窗口

9

好的,我正在尝试按照这篇文章的建议使用控制器来正确调用我的页面上的模态弹出框。

ASP.NET MVC 模态对话框/弹出窗口 最佳实践

我参考了以下内容:

http://microsoftmentalist.com/2011/09/14/asp-net-mvc-13-open-window-or-modal-pop-up-and-fill-the-contents-of-it-from-the-controller-method/

我有一个视图,其中包含一个下拉列表,如果用户找不到所需的项目/值,则可以建议一个新值(建议新值链接),该链接应该调用控制器并返回一个带有几个字段的弹出页面。

这是视图中的对象:

<script type="text/javascript">

        loadpopup = function () 
        {  
window.showModalDialog(‘/NewValue/New′ , "loadPopUp", ‘width=100,height=100′); 
        } 

    </script> 


<%: Html.DropDownList(model => model.ValueId, new selectlist........... %>
<%: Html.ActionLink("Suggest Value", "New", "NewValue", null, new { onclick = 'loadpopup()') %>

我计划使用的控制器以返回页面:
public class NewValueController : Controller{
   public Actionresult New(){
      return View();
   }
}

我现在陷入了困境。我想返回一个可以格式化的页面,是不是必须要返回一个字符串?不能够返回一个aspx(我使用的引擎)吗?因为这样更容易进行格式化。

非常感谢任何关于我应该采取哪个方向的建议。

谢谢!

2个回答

17

您可以使用jQuery UI对话框来实现弹出窗口。这里有一个小的设置:

我们将为主表单创建一个视图模型:

public class MyViewModel
{
    public string ValueId { get; set; }
    public IEnumerable<SelectListItem> Values 
    { 
        get 
        {
            return new[]
            {
                new SelectListItem { Value = "1", Text = "item 1" },
                new SelectListItem { Value = "2", Text = "item 2" },
                new SelectListItem { Value = "3", Text = "item 3" },
            };
        } 
    }

    public string NewValue { get; set; }
}

一个控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content("thanks for submitting");
    }
}

和一个视图 (~/Views/Home/Index.aspx):

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" 
%>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <% using (Html.BeginForm()) { %>
        <%= Html.DropDownListFor(x => x.ValueId, Model.Values) %>
        <br/>
        <%= Html.EditorFor(x => x.NewValue) %>
        <%= Html.ActionLink("Suggest Value", "New", "NewValue", null, new { id = "new-value-link" }) %>
        <button type="submit">OK</button>
    <% } %>

    <div id="dialog"></div>

</asp:Content>

那么我们可以处理弹出窗口。我们为其定义一个视图模型:

public class NewValueViewModel
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

控制器:

public class NewValueController : Controller
{
    public ActionResult New()
    {
        return PartialView(new NewValueViewModel());
    }

    [HttpPost]
    public ActionResult New(NewValueViewModel model)
    {
        var newValue = string.Format("{0} - {1}", model.Foo, model.Bar);
        return Json(new { newValue = newValue });
    }
}

还有一个对应的部分视图(~/Views/NewValue/New.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.NewValueViewModel>" 
%>

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "new-value-form" })) { %>
    <%= Html.EditorFor(x => x.Foo) %>
    <%= Html.EditorFor(x => x.Bar) %>
    <button type="submit">OK</button>
<% } %>

现在只需要编写一些JavaScript代码来连接所有东西。我们会引入jQuery和jQuery UI:

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery-ui-1.8.11.js") %>" type="text/javascript"></script>

还需要一个自定义的 JavaScript 文件,其中将包含我们的代码:

$(function () {
    $('#new-value-link').click(function () {
        var href = this.href;
        $('#dialog').dialog({
            modal: true,
            open: function (event, ui) {
                $(this).load(href, function (result) {
                    $('#new-value-form').submit(function () {
                        $.ajax({
                            url: this.action,
                            type: this.method,
                            data: $(this).serialize(),
                            success: function (json) {
                                $('#dialog').dialog('close');
                                $('#NewValue').val(json.newValue);
                            }
                        });
                        return false;
                    });
                });
            }
        });
        return false;
    });
});

尝试过了,但目前它不是弹出窗口,而是打开一个新页面。想知道jQuery的哪个部分会触发模态框? - gdubs
这是对链接的.click()事件的订阅。请确保您已正确包含了所有3个JavaScript文件(jquery-1.5.1.min.jsjquery-ui-1.8.11.jsmy.js - 按照我展示的顺序包含脚本),并且JavaScript控制台中没有错误。 - Darin Dimitrov
发现问题了,是因为多了一个' }); '哈哈!再次感谢! - gdubs
顺便说一句,很抱歉还有后续问题。但是,我想用我添加的新值更新<%= Html.DropDownListFor(x => x.ValueId, Model.Values) %>。返回Json(new { newValue = newValue });会处理吗?它将如何成为下拉列表的selectlistitem,只是好奇。 - gdubs
你可以在成功处理程序中向下拉菜单添加一个新的<option>项,而不是设置NewValue字段。 - Darin Dimitrov
嘿,抱歉回复晚了。我尝试刷新页面来解决问题,但是感觉最好还是刷新下拉菜单?希望列表有序,所以刷新下拉菜单是理想的选择,除非只能在底部添加额外选项,那么我可能会采取那种方法。 - gdubs

-2
$('#CheckGtd').click(function () {
    if ($(this).is(":checked"))
        $("#tbValuationDate").attr("disabled", false);
    else
        $("#tbValuationDate").attr("disabled", "disabled");
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接