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");
        }
    }`
6
  • 1
    RegisterStartupScript() is web forms code, not MVC. Use javascript. Commented May 18, 2017 at 1:10
  • please ref Can i return javascript from MVC controller to View via Ajax request return JavaScript ... Commented May 18, 2017 at 1:18
  • 2
    You should returning error message as JavaScriptResult through AJAX request, with alert method showing message box in client-side with JS. Commented May 18, 2017 at 1: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 Commented May 18, 2017 at 2:39
  • How are you even referencing ScriptManager in an ASP.NET MVC Application? stackoverflow.com/questions/4740326/… stackoverflow.com/questions/16587704/… Commented May 18, 2017 at 2:43

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

My pleasure @Mike and am really happy that it helped you. Hope it will be helpful for the others too in the future..:)
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 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
Thank you so much sir :)
Again my pleasure and happy sharing sir..:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.