MVC Routes & Subfolders - asp.net-mvc-4

I'm trying to figure out a way to customize a Route that will allow me to use a subfolder within a particular View folder.
I have a Controller (FinanceAdmin) and a View folder (\FinanceAdmin) which contains a number of Views. Within that view folder, I have a lot of stand alone chart Views (Chart1, Chart2...Chart50, etc...) which I include as Partials on various View pages. To clean things up in my file/organizational structure, I would like to set things up like this:
I know I can use Areas to separate different parts of my application but that's not really what I'm looking for. I want to be able to create a custom Route so that, in my controller, I can simply return:
return View(chartdata);
instead of
return View("~/Views/FinanceAdmin/Chart/_Chart1.cshtml",chartdata);
Is that possible with a generic route (so I don't have to create one for each file)? I'd rather not write a custom view engine just for this unique circumstance.

I am afraid that this is not possible with a route. The routing engine finishes his responsibility at the time he finds (or doesn't find) a controller action to be executed given some request url.
Resolving views is purely the responsibility of the view engine. So if the conventions built into the view engine you are using do not meet your specific requirements, customizing this view engine is the right way to go.

Related

How to have methods in common for several (partial) views

in my asp.net core web project i have two partial views that are displaying products. the first one shows them in a big layout, while the second one displays them in a smaller, regular layout.
both partial views need a method that composes a SEO-friendly url (using the product name and further information which are provided from the ViewModel). this method should somehow be accessible to the both views.
where can i place it inside of the asp.net core application? are there any particular conventions that must be followed?
#Html.Partial("ViewName")
Uses a view in current folder with this name. If none is found, searches the Shared folder
#Html.Partial("ViewName.cshtml")
A view with this name must be in the same folder
#Html.Partial("~/Views/Folder/ViewName.cshtml")
#Html.Partial("/Views/Folder/ViewName.cshtml")
Locate the view based on the application root. Paths that start with "/" or "~/" refer to the application root
#Html.Partial("../Account/LoginPartial.cshtml")
Locate the view using relative paths

Where is the equivalent of WebForms' Master Page codebehind files in ASP.NET MVC?

Today is my first day working with MVC and I am trying to convert my existing Web Forms website into an MVC 4 site.
I have done some reading and am starting to understand how things work but one thing I can not figure out is for the new Layouts (replacing MasterPages) where is the equivalent to the codebehind file? In my current site I have a Master Page that defines the general look and feel but also runs some code in the codebehind to changes a few things dynamically (for localization and DB generated menu system).
So now that I am using MVC and Layouts I can not figure out where I would code all that at, can anyone please point me in the right direction?
(I know MVC does not have code behinds it uses controllers for it.)
As you Know MVC is three layer architecture.
Model
View
Controller
Model is the data entities. You need to store, or show the data.
Views are the html or presentation layer which would be rendered to users.
Controller are the code behind file all of your code would go in controller. It gets data from Models and apply business logic and then pass to views to show or get updated data from view and pass to models and then save to database.
_layout.cshtml file is present at path of ~/Views/Shared/_Layout.cshtml. It is master-page in mvc. You would see your partial-views contains
Layout = "~/Views/Shared/_Layout.cshtml";
this line at top of page. You can change master-page for any views and you can have multiple Layouts.
Layout contains many partial-views like left-navigation, Top-Navigation and content. each of which can be customized from controller.
Here are some links might help you:
MVC Tutorials
Introduction to MVC
Create a Base Controller class and make all your controllers inherit from it.
The MVC equivalent of WebForms' Master Page codebehind is then this Base Controller, where you can put code you need for multiple controllers.
How can I execute common code for every request?
You can't find any examples of what you're trying to do, because that's not how it's done in MVC. There is no equivalent to code behinds.
You're "trying to do" the wrong thing. MVC layouts are simply template files. They have no code behind, and they should have no functionality besides simple display logic.
MVC is a different paradigm from WebForms. You don't use have server-side controls like WebForms. Therefore the idea that you have content in the layout that does it's own thing violates the MVC principles.
You're basically stuck in what's known as the XY problem. That's where you are trying to achieve certain functionality X, and you believe to do that you need to do Y, so all you ask about is Y... when X is what you really need to be asking about.
Please explain the actual thing you are trying to do, and don't assume that it must be done in the way you've always done it. For instance, if you want to localize something, then ask how to localize something. If you want dynamic content somewhere, ask how to do that, but you need to be more specific about these individual problems, and not just gloss over them as you have done here.

Where does ExtJS Action fit into MVC pattern

I really like concept of ExtJS Actions. I looked at this example and it is (almost) exactly what I need. Only thing is that I'm trying to use MVC pattern.
I have:
invoicelist (view)
Inovice (controller)
Invoice (model)
Invoices (store)
Where and how do I put definition for Action? Should they be in controller? How to call them and reference them? I need several Actions and they will be in context menu and in menu in invoicelist's toolbar.
Good question. It seems Actions break the MVC pattern by somehow combining View and Controller paradigms under one roof. Because they have handlers they carry functionality with them as well as UI elements like text and icons. However they are not components - in ExtJS sense of the word. Hence you can't target them with a selector.
The best way to think of them is as a config object. No more, no less. A config object is meaningless by itself - and can not be targeted. Same with Actions. They can actually be used as a config object to buttons for example.
Now where should they go? The answer to that I guess is really up to you as a designer. Since they don't confirm to strict MVC pattern you get to make a decision based on how widely you need a particular action be accessible. For a truly global action that is shared by many views you might even put it in the application config: MyApp.app.actions["delete"] for example.
Controller might be a good place to put it if that controller will configure multiple views and wire them together with stores. They can potentially wire up multiple views with shared actions.
Hope this helps. Good luck :)
just do a someview.fireEvent('Yourcustomevent');

ruby on rails 3, render multiple views in one view

I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan
If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails

ExtJS 4 MVC multiple instances of views and sub/child controller difficulties

I have encountered a problem with the MVC pattern in ExtJS 4. At least, I think I have. Having approached multiple people with this question, and having posted numerous times in the Sencha forums, I am now turning to a broader audience in hopes of getting either a light bulb or a confirmation.
Problem
Your application has the ability to open many different views, some of which themselves are mini-applications. Additionally, a user may wish to have multiple concurrent copies of a view open.
This application is a single-page client-side Javascript application.
The ExtJS 4 MVC model expects you to define all of your controllers in your Application class. These controllers are then initialized when the Application loads. Controllers keep track of views, models and stores.
When you initialize controller A multiple times, say to create more than one copy of a view, you end up with two views that reference the same data stores, and functionally send duplicate events to the Application event bus.
I have refactored my application by adding new prototype methods to Component and Controller to allow for both a) sub controllers (some of my controllers were getting pretty huge) and b) defining stores specifically for the view they work with. The models can still be defined on the controller, just for ease of use by handlers if you need to do something like grab a record from the server.
Question
My understanding of MVC would lead me to believe that models more directly relate to the View than then Controller. I asssssume that ExtJS 4 decides to attach stores (which I think can be seen as wrappers to a more classic model) to Controllers for purposes of encouraging re-use of loaded data, and to optimize away from having many copies of the same class instantiated. It seems to me, however, that one cannot do this if one intends to have many instances of a view available to the user. To my thinking, having many instances is an important option in an OO framework, hence why I have bucked the trend and implemented prototypes on some of the Ext base classes. (Thank you Ext.implement!).
Is there any way to have multiple concurrent instances of a view with different data loaded into them using the out of the box MVC classes and making uses of the provided setters, getters, etc?
I was faced with a similar problem:
Consider a tabpanel for a CRM type application which opens new instances of a view for each client. And say that tab view contains 3 or 4 row-editing gridpanels for interacting with different collections of data relating to that client.
The solution I came up with was based on this from the Sencha forums. In a nut shell, almost all events that are dispatched from a view contain a reference to the view itself. The handlers in my controller's control function all use these to get a reference to the correct view instance.
For dealing with the multiple instances of the same store needed for this, I took this to heart from that post:
For the Store instance on the view or a global one... depends on the
needs. If you are going to use globally then make it global. If you
only are going to need it on the view then put it on the view. MVC is
not a law, you can change it to fit your needs. Technically the
Controller part of MVC is suppose to be the middle man between the
View and Model parts but sometimes that's just not needed. I create
the Store in the view 95% of the time. I'll give you an example...
If you have a Store for products, you probably only need to reference
that Store in your Grid. That usually isn't needed for other parts of
the application. However, if you have a Store to load countries, I
often need it globally so I only have to load it once and can then
set/use that Store in several views.
So I just created the needed store's that relate to a view instance specifically, inside the view's initComponent method. The application did have a few global stores that I created as store classes following the MVC recommendations. It worked out nicely to encapsulate the view instance stores inside the view. Then I only needed one instance of the controller.
To answer your question specifically, currently, there is no ExtJS official recommendation or config for dealing with multiple instances of the same view that use the same store constructor. I have spent some time looking for something like that and the best I have found was this recommendation from one of their forum moderators.
I don't think you ever need more than 1 instance of a controller, regardless of how many views/models you have. See functional example here:
http://whatisextjs.com/extjs-4-extension/fieldset-w-dynamic-controls-7
This can be done, reasonably easily. You need to follow a few rules:
load your controllers at app startup. Don't unload them. Don't worry about the memory or time, it's pretty small even for hundreds of controllers, as long as you minimize and concatenate your js.
Never use the refs or views properties of a controller. You are going to use one instance of a controller, but multiple instances of views, so you never want a reference to a view.
only use event listeners in controllers. You are only going to listen to events on your views. You can always get a (temporary) reference to a view in the event handler via the "cmp" parameter in the handler.
To "launch" a view, create it and add it to another view. To destroy it, destroy it. You don't use a controller to launch a view. You can use the afterrender and beforedestroy events in the controller to add logic.
In ExtJS' MVC the controller is a Singleton for you view. I like how DeftJS thinks about MVC. Each instance of a view has an own instance of a controller. In this way you can put all "controlling rules" in a controller for a particular part of your view, and this will be instantiated only when the view opens.
I did not have any experience how I could use multiple Defts JS apps in the same project.
Of course. What led you to believe otherwise?
Here is an example of creating a custom View which extends from a Window component. You can run this method many times from the same controller and each time you will get a new instance of a View.
"this" refers to a controller that code runs in:
this.getRequestModel().load(requestID,{ //load from server (async)
success: function(record, operation) {
var view = Ext.widget('requestEdit',{
title: 'MyRequest '+requestID
});
var form = view.down('form');
form.loadRecord(record);
}
});
How do you create your views? I see no reason why you cannot pass a different store or config data to every object. Some code samples would help for what exactly you are doing. For example, we have a similar sounding application, and everything is done with extensions. So, if we need a grid, we run
Ext.define('MyApp.grids.something',{
extends:'Ext.grid.panel'
//...
These classes are predefined. Then, when a controller or view is loading this grid, they are using
var grid=Ext.create('MyApp.grids.something',{id:'unique',store:mystore});
As you can see, we can pass in different config options to the same grid each time it is created. We can treat this exactly as you would treat
Ext.create('Ext.grid.Panel');
Except of course that we make some options predefined, and some non-override-able, and so on.
Hope this helped.
Check out this post. The idea there is to take some configuration (like store and itemId) from view config and put it into the viewport config:
// .../app/view/Viewport.js
Ext.define('MyApp.view.Viewport', {
// ...
items: [
// ...
{ xtype: 'testview', store: 'Store1', itemId: 'instance1' },
{ xtype: 'testview', store: 'Store2', itemId: 'instance2' }
]
});
The problem with store will be solved, obviously. Different itemIds will enable you to handle events properly.