Continuing the article that has told you about how to produce output to the web page using Redirect Action Method in ASP.NET MVC. Today, I will show you how to produce output to the web page from controller using JavaScript Action Method.
JavaScript() action method returns JavaScript code to the browser. This method returns JavaScriptResult action result that derives from ActionResult class.
public JavaScriptResult OutputJavaScriptAlert()
{
string a = "alert('this is alert')";
return JavaScript(a);
}
To test this, create a view with following code:
Try clicking on the link below
@Ajax.ActionLink("Test JavaScript", "OutputJavaScriptAlert", new AjaxOptions{UpdateTargetId = "divLink"})
Now, after running your project try clicking on the link and you will get an alert like below.
Simple, right? That's the way to produce output in ASP.NET MVC using Redirect Action Method. Just look forward to further discussion in the next article.
HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.