在ajax发布后从控制器呈现警报

时间:2017-04-22 17:59:19

标签: jquery ajax asp.net-mvc twitter-bootstrap alert

我正在进行ajax调用以发布我的数据

                $.ajax({
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: 'POST',
                url: '/Interview/SaveInterview',
                data: totalInterview,
                success: function (response) {
                    //$('#divNotification').html(response);
                },
                failure: function (response) {
                    alert('failed');
                }
            });

它调用控制器方法

        public ActionResult SaveInterview(List<InterviewViewModel> totalInterview)
    {
        //saves successfully.......

        if (blnSaveResult)
        {
            Success(string.Format("<b>{0}</b> was successfully added to the database.", "Interview"), true);
        }
        else
        {
            Danger("Oops! Something went wrong! Please check your form and try again.");
        }

        if (Request.IsAjaxRequest())
        {
            //return View("AddInterview");
            return PartialView("_Notifications");
        }
        else
        {
            return View("AddInterview");
        }
        //return this.Json(new { success = blnSaveResult }, JsonRequestBehavior.AllowGet);
    }

成功方法:

        public void Success(string message, bool dismissable = false)
    {
        AddAlert(AlertStyles.Success, message, dismissable);
    }

        private void AddAlert(string alertStyle, string message, bool dismissable)
    {
        var alerts = TempData.ContainsKey(Alerts.TempDataKey)
            ? (List<Alerts>)TempData[Alerts.TempDataKey]
            : new List<Alerts>();

        alerts.Add(new Alerts
        {
            AlertStyle = alertStyle,
            Message = message,
            Dismissable = dismissable
        });

        TempData[Alerts.TempDataKey] = alerts;
    }

这只是一种将数据传递到局部视图(_Notifications)以显示警报的方法。

_Notifications.cshtml

@{
     var alerts = TempData.ContainsKey(Alerts.TempDataKey) ? (List<Alerts>)TempData[Alerts.TempDataKey] : new List<Alerts>();
if (alerts.Any())
{
    <hr />
}

foreach (var alert in alerts)
{
    var dismissableClass = alert.Dismissable ? "alert-dismissable" : null;
    <div class="alert alert-@alert.AlertStyle @dismissableClass">
        <div class="container-fluid">
            @if (alert.Dismissable)
            {
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true"><i class="material-icons">clear</i></span>
                </button>
            }
            @Html.Raw(alert.Message)
        </div>
    </div>
}

}

应该在我的主视图中调用并显示警报。它只是没有表现出来。我不知道为什么。 我尝试渲染局部视图,不起作用。

我的主视图呈现我的部分(警报)视图:

@{Html.RenderPartial("_Notifications");}

我需要做什么?

0 个答案:

没有答案