How to set the View  Name in my controller in MVC

Posted on 2014-03-27
Medium Priority
Last Modified: 2014-03-28
I am creating an application using TDD with MVC 4.  I have a very simple HomeController and Index action that simply returns a View.  This view displays a menu to the user.  Here is the controller code:

using System;
using System.Web.Mvc;
using AppCatalog.Models;

namespace AppCatalog.Controllers
    public class HomeController : Controller
        [AuthUser(Roles = "IST")]
        public ViewResult Index()
            ViewBag.Title = "Application Catalog";

            return View();


Open in new window

Here is the corresponding View:

    <!-- Set the page title text -->
    ViewBag.Label = (string)Session["labeltext"];
    <!-- Set the header [Verbose/Default/None]:
        Verbose = About, Contact, Date
        Default = Date Only
        None = Blank header (see below) -->
    ViewBag.Header = "Default";
    <!-- Display Linkback [Yes/No]:
        NOTE: Supported only with left_box/right_box -->
    ViewBag.Linkback = "Yes";

<!-- Content here will be placed in the header -->
@section header {

<!-- Menu, or title wrapped in <h2>...</h2> tags -->
@section menu_bar {

<!-- Use one_box for applications and one-column content -->
@section one_box {
<h1 style="text-align: center">@ViewBag.Title Menu</h1>

<div class="menu">
        <li>@Html.ActionLink("Database Catalog", "Index", "DBCatalog")</li>
        <li>@Html.ActionLink("User's Guide", "UserGuide", new {controller="Home"}, new { target = "_blank" })</li>

Open in new window

I have a unit test that simply wants to verify the View returned is the Index view but the test is failing.  When I debug the test and look at result.ViewName, it is null.  Here is the unit test:

        public void Index_Get_AsksForIndexView()
            // Arrange
            var controller = new HomeController();

            // Act
            ViewResult result = controller.Index();

            // Assert
            Assert.AreEqual("Index", result.ViewName);

Open in new window

Can someone please tell me how I can verify the Index View is being returned?
Question by:dyarosh
LVL 52

Accepted Solution

Carl Tawn earned 2000 total points
ID: 39960173
Basically you can't. If you don't specify the view name when returning it (i.e. return View("Index");) then you can't check the name.

However, you shouldn't need to because if you don't specify a view name then the view name is by definition the same as the action name.

Author Closing Comment

ID: 39961314
Thank you.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question