Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I cause a javascript visual effect from a controller when specific conditions are met?
    primarykey
    data
    text
    <p>I am currently writing a ASP.MVC application. I am almost done with it. I am now doing the look and feel of the website. The problem is that I have piece of javascript in my view which I would like to call from my controller when a specific condition is met.</p> <p>Here is the code examples.</p> <p>MenuController.cs:</p> <pre><code>[HttpPost] public ActionResult Create(FormCollection collection,Models.Menu sentData) { try { // TODO: Add insert logic here //db.Menus.AddObject(sentData); int existingQuery = (from m in db.Menus where m.Weekstart == Helper.GetNextMonday() &amp;&amp; m.category == sentData.category select m).Count(); if (existingQuery &gt; 0) { throw new Exception(); } Models.Menu MenuItem = new Models.Menu { Monday = sentData.Monday, Wednesday = sentData.Wednesday, Friday = sentData.Friday, category = sentData.category, Weekstart = Helper.GetNextMonday(), }; db.SaveChanges(); return RedirectToAction("Index"); } catch { //Javascript visual here return View(); } } </code></pre> <p>and here is the create view for this method.</p> <p>/Menu/Create.aspx</p> <pre><code> &lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;Lunch_Ordering_Application.Models.Menu&gt;" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; Create &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt; Create&lt;/h2&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;%: Html.ValidationSummary(true) %&gt; &lt;fieldset&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.Monday) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextBoxFor(model =&gt; model.Monday) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Monday) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.Wednesday) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextBoxFor(model =&gt; model.Wednesday) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Wednesday) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.Friday) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextBoxFor(model =&gt; model.Friday) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Friday) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.category) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.DropDownListFor(model =&gt; model.category,new SelectList(new List&lt;string&gt; {"Nothing","Traditional","Chill Out","Healthy Lifestyle","Vegitarian","Salad"})) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.category) %&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%: Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; &lt;script src="/js/all.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function () { ReadyMade.init(); $('.growl_trigger').live('click', function (e) {e.preventDefault(); $.msgGrowl({type: error, title: "Error!", text: 'The category for this list is already listed for next week Monday! Please change the categories here.'}); }); &lt;/script&gt; &lt;div class="msgGrowl-container bottom-right"&gt;&lt;/div&gt; &lt;/asp:Content&gt; </code></pre> <p>The javascript is at the bottom of the page. I want to attempt to call it if an exception occurs in the controller. Ignore the actual error message. Is this even "remotely" possible? Or is there an easier way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload