2015-06-07 115 views
0

,我想在我的MVC項目中使用flash messages,以下說明:閃存的消息在MVC

我用的NuGet添加的包Install-Package FlashMessageExtension,並且是創建了一些項目:

  • FlashM essageExtensions.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> 
+0

請[edit]添加一個特定的問題陳述 - 「不起作用」可以假定,但* how *不起作用?什麼錯誤信息或不正確的行爲是特徵? –

+0

然後讓我發佈所有代碼。 – developer033

+0

完成。添加了所有錯誤,你想看看生成的代碼嗎? – developer033

回答

1

您需要包括jQuery的,以及閃光燈消息腳本:

<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始終在所有其他腳本之前加載。

+0

我已經在我的_Layout中擁有這個'@ Scripts.Render(「〜/ bundles/jquery」)',無論如何我添加了你的建議,並且仍然給出錯誤。 – developer033

+0

是否在cookie和flashmessage的腳本標記之前或之後出現? –

+0

我發現問題,@ Html.Partial應該在所有腳本之後。謝謝你的嘗試。 +1 – developer033