View or Partial View - view

How can I find out whether a view (i.e: t.cshtml) is a partial view or a normal view?
Is there a sign or any other thing that differentiates between them?

In Razor you should not talk about views and partial views. They are both templates. And some templates might have layouts (standard views) whereas others not (partials). So you could check whether the Layout property is defined:
#string.IsNullOrEmpty(this.Layout)
If the layout is null or empty you could suppose that this is a partial view. But as I said there's no longer this notion of partial views in Razor. It's just a template without a layout.

Related

MVC 3 Combinging multiple controls in one sort control template

I'm working on some forms, and often I can reuse the same amount and combination of fields together, is it possible to group them all as a template and then call it from the page?
For example:
2 radio buttons with labels and 2 texboxes under.
How is it called so i can do a proper research?
You can use Partial View, its like a reusable component.
These links will elaborate more:
http://www.devcurry.com/2012/04/partial-views-in-aspnet-mvc-3.html
http://www.dotnet-tricks.com/Tutorial/mvc/2IKW160912-Partial-View-in-Asp.net-MVC3-Razor.html
MVC3 Partial Views
Create a Partial View and keep your markup which you want to reuse in that. You can use these Partial views in other views as needed then.
You can use the Html.Partial helper method to call the partial views in other views.
#Html.Partial("RecentItems")
You can also pass some model to your partial view as well
#Html.Partial("RecentItems", Model.RecentItems)
Assuming Your partial view is strongly typed to a class which is of the same type as the type of Model.RecentItems, of the caller view.

Mvc base viewmodel render data within layout

I have created a base viewmodel that all of my view models inherit from. That part is easy.
All views are bound to a viewmodel (all are inherited from the base view model)
Within the OnActionExecuted method I insert a true/false value onto a property within the baseviewmodel depending on some conditions.
From the view side of things. I have a single layout page that some how I want to be able to read the property's value and render a different partial view based on the value.
Is this possible? I dont want to have to add the code to each of the views but I don't think I should be binding the layout to my baseviewmodel either.
If I can stay away from inserting the value into the valuebag that would be great as I need to be able to access these values anywhere in the application via strongly typed names.
what you want probably isnt possible because as you call a view from the controller then first the code inside that view is executed and then the layouts code is executed
to achieve what you are doing you can do 2 things
1. make the logic inside the controller itself and then render the correct view
2.call the layout from the controller giving it the name of the partial view in some property of the model or in the viewbag
Not sure I quite follow the use case, but rather than trying to render out a partial view, have you thought about nesting your layout pages.
I think you should be able to override the layout in the onactionexecuted, so you can set the layout dependant on the bool and that layout will render only the correct option.
Look here for an example:
Nested layout pages with Razor
HTH
Si

Using view model in Partial View defined in Layout

This is all hypothetical, and wondering how and if this is possible.
I have two different strongly-typed views that use two different models. For example, I have a View1.cshtml, and View2.cshtml. Each has it's own model: View1Model.cs and View2Model.cs. Each of the view pages uses a common layout (_Layout.cshtml) that has a PartialView (_WhoAmI.cshtml).
In the View1Model.cs there is a UserID field (with various other fields) and the View2Model.cs also contains the same UserID (with different fields than View1Model.cs).
The partial view will lookup the UserID and display the User's information in a small display .DIV.
What is the best way to do this and keep the PartialView strongly-typed? Is it even possible when both models of the view are so different (other than the UserID)? If so, if I use this PartialView again in the _Layout, how do I keep it from doing the User lookup two times in the Layout page for each time the partial is called?
Thanks in advance!
Instead of Html.Partial you could use Html.Action and have a child action rendering the partial and doing the necessary lookups.
I assume that if partial view has model of type dynamic then what you ask might be possible, but haven't tried it myself. At least MSDN says that dynamic variables are bypassing static type checking and at runtime they are only checked for existence of specific methods that were called from code.

why Page is not available under Razor

I move aspx views to razor. Some things are not worked (null reference) :
Page.RouteData.Values["IdeaType"]
Page.User.IsInRole("Admin")
I have to change to:
ViewContext.RequestContext.RouteData.Values["IdeaType"]
ViewContext.RequestContext.HttpContext.User.IsInRole("Admin")
why?
need to import something?
Razor's Page property returns a page state bag that can pass data between layout pages, content pages, and partial views.
WebForm's Page property returns your Page instance (it's inherited from the Control class).
They're not the same.
In general, WebForms properties and Razor properties are rather different.
Note that Razor pages also have a User property; you can just write User.IsInRole(...).

how to render a razor partial from inside an aspx master pages

I've just read the following article
http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
it outlines an interesting technique/hack to override the "View" method to allow razor views to replace aspx views. I'm wondering if anyone has a similar override for partials?
Are you just trying to render the partial from within a view? If so - you shouldn't need to do anything differently. Calling Html.Partial or Html.RenderPartial from you aspx view should render a razor partial.

Resources