Reputation: 549
When I add a new ActionResult in controller like this
public ActionResult Step8(AddPropertyStep7ViewModel model)
{
return View();
}
I need to specify what kind of view I want to return.{View with layout, View , Partial View}
I know that I want to return a file like this one below
This is the Step1.cshtml. How do I determine whether it is a view or a partial view of sth?
@using W.Resources
@model W.Models.ViewModels.Agent.AddPropertyStep1ViewModel
@{
Layout = "~/Areas/Agent/Views/Shared/_Layout.cshtml";
}
<div class="side site--dashboard"> ... <div>
@section scripts
{
<script> ... </script>
}
Upvotes: 0
Views: 35
Reputation: 239290
Views are views are views. The terms "layout" and "partial" just make it easier to talk about how a view is being used, but there's no functional difference between them. In other words, a layout is a view used as a layout. A partial is merely a view used as a partial. There's no way to just look at a view and know what type of view it is because it's entirely contextual.
That said, layouts are kind of an exception, since in order to truly function as a layout, they need @RenderBody()
to be called somewhere. However, you could still use a view that didn't call this as a "layout" for another view. It just wouldn't actually render the view's HTML: all that would be returned is the layout's HTML.
There is a convention of prefixing layouts/partials with an underscore. However, that is merely a way to quickly see within the project tree that there's something special about a view file. It's not required and doesn't really give you any information about what exactly is special about the view.
That said, your question here isn't entirely clear. You say you want to specify what type of view is returned, presumably from your action. Well, you're already doing that, in effect. When you return View()
, you're saying you want the view to be treated like a standard view. If you wanted to return a partial, you would have to return PartialView()
instead. There's no way to specify that the returned view must utilize a layout, though.
Upvotes: 1
Reputation: 5989
The basic way to define partial view is that it has Layout = null.
BUT
even if you have value in your Layout
property, and you return the view like following from controller.
return PartialView("Your_view_name");
it will still get rendered as a partial view (it will not get wrapped in master page content).
As MVC works by Convention over configuration, the convention to define partial view is that the view name should start with "_" (_MyPartialView.cshtml)
As you mentioned in your edited question you need to specify which kind of view you want to return. i don't see need where you just need to return layout and nothing else. the solution i proposed above either to use return View()
or return PartialView()
from your action method will work perfectly and you can decide if you want to render page with master page or as partial view.
Upvotes: 1