Export (0) Print
Expand All

Controller.View Method

.NET Framework 4

Creates a ViewResult object that renders a view to the response.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

protected internal ViewResult View()

Return Value

Type: System.Web.Mvc.ViewResult
The view result that renders a view to the response.

This method overload of the View class returns a ViewResult object that has an empty ViewName property. If you are writing unit tests for controller actions, take into account the empty ViewName property for unit tests that do not take a string view name.

At run time, if the ViewName property is empty, the current action name is used in place of the ViewName property.

This method returns the view that matches the action result name from the Views subfolder that in turn matches the controller name. For example, if you are using the default view engine, the action result TestJson in a controller named PRGController returns the following view:

Views\PRG\TestJson.aspx

The following example shows how to return a view from an action method. Both action methods return the TestJson.aspx page from the Views subfolder that matches the controller name. The TestJson method defaults to returning the TestJson.aspx page. The TestJsonContent method explicitly returns the TestJson.aspx page.

public ActionResult TestJson() {
    return View();
}
public ActionResult TestJsonContent() {
    return View("TestJson");
}

Community Additions

ADD
Show:
© 2014 Microsoft