1

I have a controller that returns an ActionResult. Specifically, it calls return View(someViewModel) at the end of the method. Here's the method signature:

protected ActionResult SomeControllerMethod(AViewModel someViewModel)

I've subsequently inherited from AViewModel (AnInheritedViewModel), added a few new properties to the class and am now passing it into SomeControllerMethod.

Now, at the return statement at end of this method I get an error about how the view can't be found. That's fair enough, but I'm not sure how this all works by default.

The view names MVC tells me it's looking for don't line up with the name of either the controller method or the model type. Going by the same pattern, there are no views corresponding to the name of the original model either. So I'm not sure how MVC decides which view it's going to use?

3 Answers 3

6

When you

public ActionResult SomeControllerMethod()
{
    return View();
}

MVC tries to look for a view named "SomeControllerMethod.cshtml". It uses the name of the method as a guide for looking for a view file.

You can override this by:

public ActionResult SomeControllerMethod()
{
    return View("MyView");
}

and MVC will thus try to find "MyView.cshtml".

Sign up to request clarification or add additional context in comments.

4 Comments

Wouldn't this just send the string 'MyView' as the model to SomeControllerMethod.cshtml?
To send a model, you would use: return View("MyView", myModelClass);
Thank you Jason (and everyone else). What if (as in my case) the call is to View(model)-- does MVC use the model's class type to determine the view to look for?
If the action method is called "Index" and you call return View(model), then MVC will look for a view named "Index.cshtml" and pass the model to it.
4

Also be sure to watch out for views that live in /Views/Shared (MVC will also search this folder by default). But the most annoyingly one is when the action method has been renamed using the ActionNameAttribute, e.g.:

[ActionName("Bob")]
public ActionResult MyMethod()
{
    return View();
}

In which case MVC will go looking for a view called "Bob".

Comments

1

When a user enters a URL into the browser, the MVC application uses routing rules that are defined in the Global.asax file to parse the URL and to determine the path of the controller. The controller then determines the appropriate action method to handle the request. By default, the URL of a request is treated as a sub-path that includes the controller name followed by the action name. For example, if a user enters the URL http://contoso.com/MyWebSite/Products/Categories, the sub-path is /Products/Categories. The default routing rule treats "Products" as the prefix name of the controller, which must end with "Controller" (such as ProductsController). It treats "Categories" as the name of the action. Therefore, the routing rule invokes the Categories method of the Products controller in order to process the request. If the URL ends with /Products/Detail/5, the default routing rule treats "Detail" as the name of the action, and the Detail method of the Products controller is invoked to process the request. By default, the value "5" in the URL will be passed to the Detail method as a parameter. The following example shows a controller class that has a HelloWorld action method.

public class MyController : Controller
{
    public ActionResult HelloWorld()
    {
        ViewData["Message"] = "Hello World!";
        return View();
    }
}

Reference: http://msdn.microsoft.com/en-us/library/dd410269(v=vs.98).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.