How to show Message Box in MVC ASP.NET without return View()
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/
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
Related videos on Youtube
Comments
-
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 almost 7 yearsYou should returning error message as
JavaScriptResult
through AJAX request, withalert
method showing message box in client-side with JS. -
Brian Ogden almost 7 yearsI 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 almost 7 yearsMy pleasure @Mike and am really happy that it helped you. Hope it will be helpful for the others too in the future..:)
-
Mike almost 7 yearsOh 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 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 almost 7 yearsThank you so much sir :)
-
Karthik Elumalai almost 7 yearsAgain my pleasure and happy sharing sir..:)