How to show Message Box in MVC ASP.NET without return View()

17,202

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

Share:
17,202

Related videos on Youtube

Mike
Author by

Mike

I just love coding.

Updated on September 21, 2022

Comments

  • Mike
    Mike over 1 year

    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");
            }
        }`
    
    • Tetsuya Yamamoto
      Tetsuya Yamamoto almost 7 years
      You should returning error message as JavaScriptResult through AJAX request, with alert method showing message box in client-side with JS.
    • Brian Ogden
      Brian Ogden almost 7 years
      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
  • Karthik Elumalai
    Karthik Elumalai almost 7 years
    My pleasure @Mike and am really happy that it helped you. Hope it will be helpful for the others too in the future..:)
  • Mike
    Mike almost 7 years
    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?
  • Karthik Elumalai
    Karthik Elumalai almost 7 years
    @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
  • Mike
    Mike almost 7 years
    Thank you so much sir :)
  • Karthik Elumalai
    Karthik Elumalai almost 7 years
    Again my pleasure and happy sharing sir..:)