Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to set the View  Name in my controller in MVC

Posted on 2014-03-27
2
554 Views
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:

HomeController.cs
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 {
				<h2>
                	@ViewBag.Title
				</h2>
}

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

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

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:

HomeControllerTest.cs
        [TestMethod]
        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?
0
Comment
Question by:dyarosh
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 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.
0
 

Author Closing Comment

by:dyarosh
ID: 39961314
Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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