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(...).
Related
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
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.
I've been tasked with converting an existing webforms application to mvc 3 razor.
The application currently has an aspx page which has a static header user control and "n" amount of other user controls which are dynamically created. In the code behind for the file, it is executing the below code in various specific sections to dynamically process user controls with information provided from the database.
I know how to statically create partial views, but being somewhat new to MVC, how would I go about defining this new "aspx" page and also to dynamically find, load and add the partial views (each equivalent to the below webforms code)?
btw, the code will be in C# as well.
Dim parent As Control = Page.FindControl(_moduleSettings.PaneName)
Dim portalModule As PortalModuleControl = CType(Page.LoadControl(_moduleSettings.DesktopSrc), PortalModuleControl)
parent.Controls.Add(portalModule)
I think I can do something like this when the page is rendering. I want to make it as simple as possible.
The "PaneName" will be set in the parent variable which determines where in the page it will be shown (Left, Right, or Main)
The "DeskTopSrc" is the name of the partial view to display.
So, take the code out of the code behind and place it in the main View. Perform the above processing logic in the View (boy, switching from aspx code behind to a View throws me a loop. I gotta get use to doing the processing in the View. Reminds me of Classic ASP, but the Razor syntax will help).
Display the partial view via the #Html.PartialView('partial view name'). This view might have a grid in it associated with a specific model.
Below is the part I am unsure about.
I've done database processing for a main View associated with a Controller, but not with a partial view that needs to do some database processing.
Perform any database processing logic (if any) for this partial view in the Controller associated with the main View (which contains this partial View).
In the Action Index method while looping over these "partial views", I can get the data and display the views....
Ahhh, I think I got it.....
After carefully thinking it through, if someone could help me out with the last statement here, I would greatly appreciate it.
1.Have partial views already statically created with the specific HTML markup that I need in the Views/Shared folder.
2.In the main View, I will already have
#Html.Partial(ViewData["partial_view_left"])
#Html.Partial(ViewData["partial_view_right"])
#Html.Partial(ViewData ["partial_view_main"])
statements in specific locations of the HTML which will render the partial views as I retrieve their names from the database.
3.In the Controller's Index method, I need to do the following:
a) Loop through the converted logic (from the CodeBehind of the existing WebForms page in the PageLoad event) in the Index action method of the new Controller which will load the partial views dynamically.
1) Find out where the partial view will be displayed (left, right, main) from the database via the "parent" variable.
2) Find out the name of the partial view that will be displayed from the database via the “DesktopSrc” variable.
eg: ViewData["partial_view_left"] = "left_view"; OR
ViewData["partial_view_right"] = "right_view"; OR
ViewData["partial_view_main"] = "main_view";
3) Right here is where I am unsure of how to properly display the partial view.
I need to have the equivalent of a webforms "Controls.Add" method to render each partial view from the Controller that I retrieve from
the database from step 3.a.2
What statement can I use in this Index method of the Controller that will accomplish this?
In other words, if I dynamically need to display several partial views inside of a parent view, how is this accomplished in MVC?
I know for each partial view, I can send over the model associated with it, but I just don't know how I can place several partial views inside the main view page at run time from one Action method.
If your partial views need to do some processing, like database retrieval, then you should use
#{Html.RenderAction("ActionName");}
This will call an action method (which doesn't have to be on the same controller) that can dynamically choose a view based on logic, and populate the ViewModel with data from the database.
public ActionResult ActionName()
{
var modelData = GetData();
return View(settings.DesktopSrc, modelData);
}
I've seen this question asked in a couple of places and generally the answer has been "use the ViewBag" which I don't think fits our scenario.
We have a membership site which has common properties (e.g. Account -which contains the user's account settings) and I would like to use that in both the Views and _layout.cshtml (e.g. to allow the user to change the colour scheme).
In the views, we're inheriting from a base view model but how can we get access to this data in the standard pages e.g. About Us etc which don't have any associated view model? I've tried creating a partial view which passes the data through in it's controller but that didn't work
Your main options are:
1. Consider implementing a profile provider - the settings are then available everywhere, use an Action Filter
2. use your own custom context assigned to the current request/user
3. use a base controller/base viewmodel for the pages that don't have them
4. use a global action filter
5. viewdata/session
See some details at:
ASP.NET MVC 3 layout ViewBag data across all child views
if you already have this working and need access in an "About Us" page and don't want another view model (why not?) then an action filter would be the way to go - unless this is a typical aspx page in that case you are left without options 3/4 above.
I am really digging this new Razor View Engine... For someone just coming from another web platform the new MVC 3 efforts are starting to feel right at home for me...
My question is about sections, I see it is possible to define sections in your layout and inject content into them but from all the samples I have seen it appears that this is done from the view. For me it makes more sense to assign content or output for each section from the controller.
So if I have a layout that contains a side bar that I want to load widgets into my view file should not be responsible for rendering the content into those sections. If this were the case then if the side bar content was shared across multiple views then I would have to duplicate that code across view files. My view files should be ignorant of what content is rendered in the side bar next to them.
Ideally I would like to assign content to my sections in the side bar from my controller so my question, is this possible?
In ASP.NET MVC (and other MVC based web application architectures) controllers prepare the data that is rendered by the views. How, where and maybe even when this data is rendered is the view's job to figure out. This is of course more of a architectural design discussion.
A nice thing about ASP.NET MVC is their 'convention over configuration' standpoint which (especially with Razor) lets you change how a lot of things behave. I don't think there's a View.Sections["section1"] = <some data>; option, but you could make a ViewModel that is based on sections, then have some generic master view that just takes apart this ViewModel and sends the correct data to correct partial views. This view model for sections could even hold HTML that has to be rendered, as Razor can be used made to render HTML outside .cshtml files. For more go to Andrew Nures' blog: http://blog.andrewnurse.net/
As you can see this transfers a lot of responsibility from view to the controllers. I myself like that rendering is totally separated from the controllers as then I can hypothetically build very different websites just by changing how data is rendered.