2

i'm new here in stackoverflow and also new to asp.net i would like to ask how to show message box in mvc asp.net. This is my code, but it will return NullReferenceException. Thanks for your help.`

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult myfunction(MyViewModels myModel)
    {
        System.Web.UI.ScriptManager script_manager = new System.Web.UI.ScriptManager();

        if (ModelState.IsValid) {
            createRequest(myModel);
            script_manager.Page.ClientScript.RegisterStartupScript(this.GetType(), "showMyMessage", "ShowMessage('Requested Successfully.');", true);
            return RedirectToAction("GeneratePDF", "Forms", myModel);   
        }
        else
        {
            script_manager.Page.ClientScript.RegisterStartupScript(this.GetType(), "showMyMessage", "ShowMessage('Requested failed.');", true);
            return RedirectToAction("Index");
        }
    }`
Mike
  • 83
  • 1
  • 1
  • 14
  • 1
    `RegisterStartupScript()` is web forms code, not MVC. Use javascript. –  May 18 '17 at 01:10
  • please ref [Can i return javascript from MVC controller to View via Ajax request](http://stackoverflow.com/questions/19541336/can-i-return-javascript-from-mvc-controller-to-view-via-ajax-request) return JavaScript ... – Rainmaker May 18 '17 at 01:18
  • 2
    You should returning error message as `JavaScriptResult` through AJAX request, with `alert` method showing message box in client-side with JS. – Tetsuya Yamamoto May 18 '17 at 01:21
  • 1
    I suggest getting an understanding of front end coding, showing a modal/popup box in Javascript/HTML/CSS is the right way. Injecting scripts into the front end from the back wasn't a good idea circa 1999 when Microsoft ASP.NET invented this option. I have never seen someone use ScriptManger in an ASP.NET MVC app, that is a Web Forms implementation concept – Brian Ogden May 18 '17 at 02:39
  • How are you even referencing ScriptManager in an ASP.NET MVC Application? http://stackoverflow.com/questions/4740326/using-a-scriptmanager-in-razor http://stackoverflow.com/questions/16587704/how-to-use-scriptmanager-with-razor-in-mvc-4 – Brian Ogden May 18 '17 at 02:43
  • Ahh i see, thanks for the help. – Mike May 18 '17 at 03:18

1 Answers1

9

There are different ways to do the same thing, I have added three different ways and you can use, whatever required for you in different times.

Way 1: [Recommended for your requirement without return view()]

public ContentResult HR_COE()
       {


           return Content("<script language='javascript' type='text/javascript'>alert     ('Requested Successfully ');</script>");
       }

Official definition for content result class:

Represents a user-defined content type that is the result of an action method.

Source: https://msdn.microsoft.com/en-us/library/system.web.mvc.contentresult(v=vs.118).aspx

Other useful examples if required: http://www.c-sharpcorner.com/UploadFile/db2972/content-result-in-controller-sample-in-mvc-day-9/

https://www.aspsnippets.com/Articles/ASPNet-MVC-ContentResult-Example-Return-String-Content-from-Controller-to-View-in-ASPNet-MVC.aspx

Other ways:

Way 2: Controller Code:

public ActionResult HR_COE()
       {
           TempData["testmsg"] = "<script>alert('Requested Successfully ');</script>";
           return View();
       }

View Code:

@{
    ViewBag.Title = "HR_COE";
}

<h2>HR_COE</h2>

@if (TempData["testmsg"] != null)
{
   @Html.Raw(TempData["testmsg"]) 
}

Way 3: Controller code:

public ActionResult HR_COE()
      {
          TempData["testmsg"] = " Requested Successfully ";
          return View();

      }

View code:

@{
    ViewBag.Title = "HR_COE_Without using raw";
}

<h2>HR_COE Without using raw</h2>

   @if( TempData["testmsg"] != null)
   {
<script type="text/javascript">
       alert("@TempData["testmsg"]");
</script>
   }

I have used all the three ways personally and I got the output as expected. So Hope it will be surely helpful for you.

Kindly let me know your thoughts or feedbacks

Thanks Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
  • My pleasure @Mike and am really happy that it helped you. Hope it will be helpful for the others too in the future..:) – Karthik Elumalai May 18 '17 at 09:02
  • Oh one more thing, when the box shows up, after how many seconds, the page became white, what to do so that it won't became white? – Mike May 19 '17 at 01:41
  • @Mike that is because you are not redirecting to any other view(). If you dont want white screen then you should redirect to some other view or same view. For that kindly, use one of the way in other ways which is mentioned above in answer. Hope this will helps.thanks karthik – Karthik Elumalai May 19 '17 at 01:52
  • 1
    Thank you so much sir :) – Mike May 19 '17 at 02:00
  • Again my pleasure and happy sharing sir..:) – Karthik Elumalai May 19 '17 at 02:02