Skip to main content

ContentResult in MVC

Please read my articles First MVC Application and ActionResult in MVC before proceeding further in this article.
In this article we will learn about ContentResult. We can use ContentResult if we want to return some content along with content-type from an action method. The content-type can be plain text, html, css or xml.
For example , we want to return Customer Name from action method and input to action method is ‘customer id’. So based on ‘customer id’, we will return Customer Name from action method.
Below is the code of GetCustomerName() action method. We have a list of customers from which we have searched customer based on customer id and returned Customer Name from that customer object.

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

namespace MVCApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Contact()
        {
            return View();
        }
        public ActionResult GetCustomerName(int customerid)
        {
            string customerName = string.Empty;
            var customers = new[] {
                new{ CustomerId=90,CustomerName="Ayush Kumar"},
                new{ CustomerId=91,CustomerName="Utkarsh Singh"},
                new{ CustomerId=92,CustomerName="Mohit Gupta"},
                new{ CustomerId=93,CustomerName="Akshat Kumar"}            
            };

            foreach (var customer in customers)
            {
                if (customer.CustomerId == customerid)
                {
                    customerName=customer.CustomerName
                }
            }
          return new ContentResult() { Content = customerName, ContentType = 
                                     "text/plain" };
        }
    }
}

In above code ‘customers’ is an anonymous collection of customer records. We are looping through ‘customers’ collection and when input ‘customerid’ is matched with CustomerId of a record in collection then we are returning CustomerName property from that object.
For returning string property ‘CustomerName’, we are using ContentResult because now we are returning specific content with certain content-type. Here ContentType is “text/plain”.

There is another way to return Content. We can use Content() method defined in System.Web.MVC.Controller class to return ContentResult.
Below is the code:

return Content(customerName,"text/plain");

Both approach will work in same way and will return ContentResult.

Below is the modified action method:

public ActionResult GetCustomerName(int customerid)
        {
            string customerName = string.Empty;
            var customers = new[] {
                new{ CustomerId=90,CustomerName="Ayush Kumar"},
                new{ CustomerId=91,CustomerName="Utkarsh Singh"},
                new{ CustomerId=92,CustomerName="Mohit Gupta"},
                new{ CustomerId=93,CustomerName="Akshat Kumar"}                
            };

            foreach (var customer in customers)
            {
                if (customer.CustomerId == customerid)
                {
                    customerName = customer.CustomerName;
                }
            }
            //return new ContentResult() { Content = customerName, ContentType 
                                          = "text/plain" };
            return Content(customerName,"text/plain");
        }

Please note return type of action method GetCustomerName() is ActionResult, since ContentResult is child class of ActionResult so we can return ContentResult from this action method.

Now press F5 to start debugging. We will provide customerid in querystring. Input customerid 90 and press enter key. Please see below screenshot:

Please see above screenshot, querystring ‘customerid’ is 90 and corresponding to that customerid , object in customers collection is selected and CustomerName ‘Ayush Kumar’ is returned from action method.

Leave a Reply

Your email address will not be published. Required fields are marked *