1

我试图flash messages按照以下说明在我的 MVC 项目中使用:

Install-Package FlashMessageExtension通过 nuget 添加了包,并创建了一些项目:

  • FlashMessageExtensions.cs;
  • _Flash.cshtml;
  • jquery.flashmessage.js;
  • jquery.cookie.js;

正如说明所说,我将它添加到我的 _Layout.cshtml 中:

<script src="@Url.Content("~/Scripts/jquery.cookie.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.flashmessage.js")"></script>
@Html.Partial("_Flash")

在我的控制器中,我正在调用:

using MyProject.Extensions;
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name")] Model model)
{
   if (ModelState.IsValid)
   {
       db.Model.Add(model);
       db.SaveChanges();
       return RedirectToAction("Index").Success("Create was successful");
   }
   return View(model);
}

由于某种原因它不起作用,有人可以帮助我吗?

错误

SCRIPT5009: 'jQuery' is not defined
jquery.cookie.js, line 60 Character 1

Line: jQuery.cookie = function (key, value, options) {

SCRIPT5009: '$' is not defined
jquery.cookie.js, line 1 Character 1

Line: $.fn.flashMessage = function (options) {

SCRIPT5009: '$' is not defined
Create, line 149 Character 5

Line: $("#flash-messages").flashMessage();

生成的代码:

jquery.cookie.js

jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jquery.flashmessage.js

$.fn.flashMessage = function (options) {
    var target = this;
    options = $.extend({}, options, { timeout: 5000, alert: 'alert-info' });

    if (!options.message) {
        setFlashMessageFromCookie(options);
    }

    if (options.message) {
        var alertType = options.alert.toString().toLowerCase();

        if(alertType === "error") {
            alertType = "danger";
        }

        $(target).addClass('alert-' + alertType);

        if (typeof options.message === "string") {
            $('p', target).html("<span>" + options.message + "</span>");
        } else {
            target.empty().append(options.message);
        }
    } else {
        return;
    }

    if (target.children().length === 0) return;

    target.fadeIn().one("click", function () {
        $(this).fadeOut();
    });

    if (options.timeout > 0) {
        setTimeout(function () { target.fadeOut(); }, options.timeout);
    }

    return this;

_Flash.cshtml

<script type="text/javascript">
    $(function () {
        $("#flash-messages").flashMessage();
    });

    $(document).ajaxComplete(function () {
        $("#flash-messages").flashMessage();
    });
</script>
4

1 回答 1

2

您需要包括 jQuery 以及 flash 消息脚本:

<script src="@Url.Content("~/Scripts/jquery.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.cookie.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.flashmessage.js")"></script>

它应该已经下载了 jQuery,并确保 jQuery 总是在所有其他脚本之前加载。

于 2015-06-07T02:34:37.537 回答