Thursday, 3 November 2016

Passing Controller Action Error to View In MVC

11/03/2016 - By Pranav Singh 0

This article will show you how you can pass or handle the controller error for displaying in view of your mvc application. This application you can use in mvc3, mvc4, mvc5, mvc6. How to handle error in controller action method and pass error to the View?

So for this article first we will create a new mvc application and add a controller class file. In this article I have used viewbag to pass the error message to view.

So here is the controller code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.Error = "This is example of error.";
            return View();
        }

    }
}

Here in above code I have stored the error message in viewbag and then pass it to display. Now create the view and add the below code into it.

@{
    ViewBag.Title = "Passing Controller Action Error to View In MVC";
}

<h2>Index</h2>
@if (ViewBag.Error != null)
{
    <h2> @ViewBag.Error</h2>
}

Here in above code I have checked weather the error viewbag is having any value or not if not then displaying the error message other wise it will not display the error message. So here is the output.





About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top