MVC Page Template Configuration in the View file itself? - c#

I want to render an MVC Page/View that will have varying widgets on the page. So some views might have Widget A, another page could have Widget A and B, etc. I was thinking each widget would be a partial view that I want to pass parameters. So if it was a weather widget, I would need to pass the partial view the Zip code etc.
My question is what the best way to approach this architecturally? We currently have an external xml file that stores this info: myview.xml (for myview.cshtm)
We don’t want to have an external file, but would like to store everything in one place maybe in the header of the view file itself? Any recommendations?

Independent widgets on your views?
Sounds like you just need to use RenderAction to render them.
More info by Haack
MSDN

Related

creating many instances of a webpage from one template?

I'm creating a new website application in asp.net. The landing page needs to have a button (or something similar) which the user can click to create a new instance of a webpage. Similar to how a Facebook user can create a new group/event or a StackOverflow user create a new question.
My website needs to be able to create multiple "events" from the landing page which can then be accessed from the landing page, each event should be a template populated with user details on creation.
Can someone please tell me how people refer to this technique of creating many instances of a webpage (event) from one template?
With ASP.NET Core MVC (using this as an example as you have an ASP.NET tag and your description doesn't specify a technology), you can create a template using a .cshtml file. If you are not familiar with these types of files (which are used within the ASP.NET framework), then I suggest a read of it here:
https://www.w3schools.com/asp/razor_syntax.asp
Roughly, it's a file with HTML content where you can easily embed .NET types (such as types from your Model) and .NET logic using "Razor syntax", so that your HTML file is modified appropriately (e.g. with queried data specific to your user) before being sent back to the client. The reference above gives good examples, so I'm not going to waste space and repeat them here.
You can have certain .cshtml files as your "template" and embed appropriate model data using Razor syntax. You can then have a hyperlink tag (for example) reference the .cshtml file using the asp-action attribute. This will render the .cshtml file to the client whenever that tag is clicked on. ASP.NET uses types called Controllers to handle such requests (Controllers are types that inherit from the Controller type) appropriately, such as querying the correct database and providing your .cshtml file with the correct data before sending the result back to the client.
ASP.NET Core MVC modularizes the types of actions described above very well (M --> Model, V --> View, C --> Controller). Here is a good reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2
For other technologies that you wish to use to achieve the same result, you will have to consult the appropriate references.
Stackoverflow is a place to get answer on specific problem with short answer. Your problem is general design and programming question, and requires understanding of basic programming aproaches.
For that you should grab a book and read about designing webapplications in .Net.

Can I override views in Web Forms like I can in MVC?

In ASP.NET MVC I can typically override view e.g. by putting a view with the same name in the DisplayTemplates folder. If I wanted to override the way images are rendered, I could put something like Images.cshtml in the folder.
Now, I want to override the way xforms are rendered in EPiServer. I know how to do this in ASP.NET MVC, but this project uses Webforms.
I have tried to search, but the documentation seems sparse on the subject. In ASP.NET MVC, I could e.g. extend the search engine to search specific locations to look for my views, or put them where ASP.NET looks by default.
This doesn't seem to work in Web Forms. Does anyone know how?
EDIT: EPiServer has an .ascx file which it uses to render an XForm with. I want to tell ASP.NET to use my .ascx file instead. To do this I need to tell ASP.NET to look for my .ascx file, e.g. by telling the ASP.NET view engine to look for my .ascx in a specific folder, or by placing it somewhere the view engine looks by default.
How do I do this?
If you want to replace it everywhere just replace the ascx file. Otherwise I am afraid the answer is no. Web Forms does not look for alternative locations for files by default as controls are usually specified using the full path or the class name. What you want to do would be equivalent of C# looking for alternative namespaces when it cannot find a class name. There are ways to achieve this behavior in Web Forms like for example Dynamic Data but ascx controls is not this.
If you are using the Property web control to display the value you can create your own custom PropertyControl and register it for your type in the PropertyControlFactory. This way you can control how your property will be rendered.
While this doesn't allow you to point out your .ascx directly, you can load it in your server control if you prefer that.
For code examples and a great summary of this (and some other) ways of customizing property rendering in EPiServer, see Mathias Kunto's blog post at http://blog.mathiaskunto.com/2012/03/05/being-friends-with-the-propertycontrolclassfactory-or-101-ways-to-change-episerver-built-in-property-appearances/.

Where to put settings of GUI/View on MVC?

On my C# 2.0 (.NET CF) program, I need to create a theme based GUI (with Controls that are non .NET and 3rd party) . I will be doing it in such a way that the user can customize the colors, fonts, toolbar/tabcontrol positions and etc. Then I need to store this into some file/XML then later on retrieve it by just using a simple serialization/deserialization. But my problem is where to put this? shall this go to model, view, or controller?
thanks
A theme based user preferences information is "data" and does not belong to any of the main MVC folders. Data is always seperate from these folders.
If you are sure that you don't want to store it in database or NO-SQL data storage, but want to use a file instead, you can create a seperate folder in the root folder for this purpose. Alternatively you can use resx files and .NET ResourceManager class if your preferences will be key-value pairs. (RESX tutorial : http://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx )
Please see below for summaries of what model, view, contoller should contain and why they are not suitable to include theme based user preferences.
MODEL: The model "represents" the data, and does nothing else. Representation is the keyword here. User theme preferences are actual data and does not belong to here.
VIEW: The view displays the model data, and sends user actions (e.g. button clicks) to the controller thus it's not suitable for storing a user preferences data.
CONTROLLER: The controller provides model data to the view, and interprets user actions. Your file clearly is not a controller and
does not belong to here.
(For a detailed explanation of what MVC concepts are and to see some examples: http://blog.codinghorror.com/understanding-model-view-controller/)

Accessing a MVC3 view via absolute URL without displaying it

What I try to achieve:
Convert a MVC3 view to PDF with abcpdf 8.
It is a result view where I want to cut off party of the views DOM like navigation and other website related parts.
Abcpdf needs, in order to render the html with all css formattings correctly, a absolute url. But I don't want to show the result-pdf view to the user. So how can I access a view from controller by URL without displaying the view. The original result view shall be displayed all the time.
Thank you in advance
Christian
You cannot access a View directly with MVC, you have to go through a Controller method. In ASP.NET MVC, URLs map to Controller methods, not to Views.
So if you know the URL of the Controller method that calls the View, you can take that URL and pass it to ABCPDF.

Offline template rendering similar to MVC partial views?

I want to refactor an existing MVC action so that it can be used offline to build email content.
The action just fetches a model by Id and hands it over to the view, where the view renders it's fields. There is a foreach loop in the view.
My first thought was to just create an html file and do string search and replace in it.
Is there any template rendering libraries I should consider instead?
You may take a look at RazorEngine.

Categories