0

我想做的事:

在页面加载时自动弹出一个 jquery 对话框,填写一些数据,将其发布到一个操作并关闭对话框(无论操作是否成功)。

在应该出现弹出窗口的视图中,我有以下内容:

<script type="text/javascript">
    $(function () {
        $('#PopUpDialog').dialog(
            {
                modal: true,
                open: function ()
                {
                    $(this).load('@Url.Action("Subscription", "PopUp")');
                },  
                closeOnEscape: false
            }
        );

        $('.ui-dialog-titlebar').hide();
        $('#closeId').live('click',function () {
                $('#PopUpDialog').dialog('close');               
                return false;
            }
        );

            $('#SubscriptionForm').submit(function () {
                $("#PopUpDialog").dialog("close");             
                $.ajax({
                    url: encodeURI('@Url.Action("Subscription", "PopUp")' ),
                    type: this.method,
                    data: $(this).serialize()
                })
                return fase;
            }
            );
    });
</script>

订阅视图具有以下内容:

@using (Html.BeginForm( new { id = "SubscriptionForm" }))
{

    @Html.ActionLink(Deals.Views.PopUp.SubscriptionResources.AlreadySubscribed, "", null, new { id = "closeId" })
    <br />
    <br />
    @Deals.Views.PopUp.SubscriptionResources.FillEmail 
    @Html.TextBoxFor(m => Model.Email)    
    <input type="submit" id="subscribeId" value="@Deals.Views.PopUp.SubscriptionResources.IWantToSubscribe"  />
    <br />    
}

效果很好。

POST 动作定义如下:

[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Subscription(FormCollection formValues)
        //public void Subscription(FormCollection formValues)
        {
            Deals.ViewModels.PopUpSubscriptionVM VM = new ViewModels.PopUpSubscriptionVM();
            TryUpdateModel(VM);
            if (!String.IsNullOrEmpty(VM.Email))
            {
                //do the update to the dbms
            }
            return Json(new { success = true });
        }

问题是,在发回后,我得到一个显示成功消息的空白屏幕,这是我不想要的!我究竟做错了什么?

4

2 回答 2

0

您可以处理成功和错误回调:

 $('#SubscriptionForm').submit(function () {
                $("#PopUpDialog").dialog("close");             
                $.ajax({
                    url: encodeURI('@Url.Action("Subscription", "PopUp")' ),
                    type: this.method,
                    data: $(this).serialize(),
                   success: function (result) {
                   //Do Whatever you want to do here 
                  },
                   error: function (x, e) {
                  //Do Whatever you want to do here
                 }
                })
                return fase;
            }
于 2012-06-04T11:25:48.143 回答
0

要查看我做错了什么,我使用以下成分设置了一个小项目(ASP.NET MVC 3):

<script type="text/javascript">

    $(function () {
        // Does not cache the ajax requests to the controller e.g. IE7/9 is doing that...
        $.ajaxSetup({ cache: false });

        var $loading = $('<img src="@Url.Content("~/images/ajax-Loader.gif")"  alt="loading" class="ui-loading-icon">');
        var $url = '@Url.Action("Subscription", "PopUp")';
        var $title = 'Some title';
        var $dialog = $('<div></div>');
        $dialog.empty();        
        $dialog
            .append($loading)            
            .load($url)
            .dialog({
                autoOpen: false
                , closeOnEscape: false
                , title: $title
                , width: 500
                , modal: true
                , minHeight: 200
                , show: 'fade'
                , hide: 'fade'
            });

        $dialog.dialog("option", "buttons", {
            "Cancel": function () {
                $(this).dialog("close");
                $(this).empty();
            },
            "Submit": function () {
                var dlg = $(this);
                $.ajax({
                    url: $url,
                    type: 'POST',
                    data: $("#SubscriptionForm").serialize(),
                    success: function (response) {
                        //$(target).html(response);
                        dlg.dialog('close');
                        dlg.empty();

                });
            }
        });


        $dialog.dialog('open');
    })
</script>

管制员的行动:

public ActionResult Subscription()
        {

            Thread.Sleep(2000); //just for testing
            TestModalAjax.ViewModels.PopUpVM VM = new ViewModels.PopUpVM();
            return View(VM);
        }


        //POST
        [AcceptVerbs(HttpVerbs.Post)]
        //[OutputCache(CacheProfile = "ZeroCacheProfile")]
        public ActionResult Subscription(FormCollection formValues)
        {
            TestModalAjax.ViewModels.PopUpVM VM = new ViewModels.PopUpVM();
            TryUpdateModel(VM);
            return Json(new { success = true });

        }

...以及相应的视图:

@model TestModalAjax.ViewModels.PopUpVM
@{
    Layout = null;
    ViewBag.Title = "Subscription";
}

<h2>Subscription</h2>
@* -----   NOTICE THE FOLLOWING!!! WITHOUT THIS DATA GETS NOT POSTED BACK!!!! ----  *@
@using (Html.BeginForm("Subscription","PopUp",FormMethod.Post, new { id="SubscriptionForm"})) 
{
    <h1> Give me your name</h1>
    @Html.TextBoxFor(M => Model.Name)
}

...所以看起来一切都按预期工作!

于 2012-06-04T20:52:31.027 回答