Is there a better way to implement forms than using RenderAction? - model-view-controller

I am working on a .Net MVC2 project where I am trying to modularize functionality as much as possible. One aspect of this would be to place a form in a partial views which is then called using RenderAction. This is so that the form's GET, POST, validation and redirection can be handled independently of the parent view.
I have read blogs and forums etc that suggest that this is not ideal behaviour for MVC, however I cannot think of a way round it. The main issue I am having is redirecting from the rendered action and I understand why I cannot do this.
What I need to know is this: if I can't and shouldn't code my modules in this way, how should I code them so they are independent of the parent view?

Related

Calling a Component from Ajax in CakePHP

I've been developing a web application in CakePHP 2.x that uses an Image cropping tool to manipulate an image. Currently it passes Ajax calls to handle the manipulation onto a function within the current Controller which then calls a Component which contains the main processing and functionality.
I'm currently going through and refactoring this section of the code and I was wondering if it's possible to directly call the Component from Ajax and whether or not it is a good idea or not as it would simplify a chunk of the code if possible.
Thoughts, opinions and tips are greatly appreciated. Thanks :)
I'm currently going through and refactoring this section of the code and I was wondering if it's possible to directly call the Component from Ajax and whether or not it is a good idea or not as it would simplify a chunk of the code if possible.
It's not a good idea because this is how components are thought to be used. A component is not thought to receive a request directly but to provide additional - reuseable functionality - to the controlller level in the MVC pattern.
Taken from the documentation:
Components are packages of logic that are shared between controllers. CakePHP comes with a fantastic set of core components you can use to aid in various common tasks. You can also create your own components. If you find yourself wanting to copy and paste things between controllers, you should consider creating your own component to contain the functionality. Creating components keeps controller code clean and allows you to reuse code between projects.

MVC - is it good practice to have multiple AJAX forms on one page

I have an MVC Razor site and looking at adding about three AJAX forms on a page, inside each form will have a partial.
Is this good practice? Should the page be limited to one AJAX form per page?
Cheers
It is fine to have multiple Ajax forms on a page. That is why you have Single Page Application (SPA) frameworks. It is not so much a technical issue, rather a conceptual issue.
Does it make sense to have multiple forms on a single page?
If the answer is Yes then proceed. I would just think about that question long and hard before proceeding forward.
inside each form will have a partial.
Not sure what you meant by that.
You can have as many AJAX forms per page as your heart desires. If the forms all post to different resources (Or controllers) there's not real clean way around it. However, if they're posting to the same resource, you might want to re-think that controller's logic.
-Just one man's opinion.
Depends on your requirement. There is nothing which stops you from having multiple forms. The usage of form is generally for triggering non-idempotent requests to the server which potentially changes some state, however there are cases where multiple forms are used on single page like a page having a details submit functionality as well as an advanced search form which displays the results in a part of the page. Also there are things like Single page Interface.

How to best apply an MVC architecture in dojo mobile (custom controllers)?

I'm pretty new to Dojo and I'm wondering about some best practises for building a MVC application. I know there are modules like dojox/app, but it seems like these are made for more complex applications.
It seems like the best way to go is to make custom page-level controller objects with will handle all the page logic, but I'm not quite sure how to fit this piece in the puzzle. What is the proper way to switch between views and passing through parameters through them following a MVC archtiecture.
I have an overview page with list items, each with their own ID. I want to navigate to another page passing through the corresponding item ID so I can retrieve the details for this item externally. What would be the proper way of doing this?
I could call a method on the corresponding controller (Page1 Controller or Page2 Details controller) directly using a button and passing the listID parameter directly?
Another way of doing this could be by working with transition states and addling listeners in the page controller to forward to the correct page. Although I'm not quite sure how to pass parameters in this scenario..
... any better solutions?
Can anybody shed some light on this? It doesn't seem like there is much documentation/examples on this with the latest versions of dojo (1.9).
Thanks!
I will recommend dojox/app since you are already using dojo mobile.
It's very simple to get started and can be use for simple apps or very complex apps
Take a look at this resources:
https://github.com/csantanapr/dapp-examples/tree/master/dapp-request
https://github.com/csantanapr/dapp-boilerplate
http://dojotoolkit.org/documentation/tutorials/1.9/dojox_app/contactsList/

ASP.NET MVC 3 Cannot find _viewstart.cshtml when using plugin/embedded views

I'm trying out a plugin mechanism for ASP.NET MVC 3 (razor) using embedded views as described here, I changed the view engine to inherit from the RazorViewEngine, but otherwise didn't really change anything. Now I'm calling a controller action in the plugin assembly, which renders a view, this works ok. But the view cannot find the _viewstart.cshtml, and thus no layout. Ideally I'd like to let the host application define the viewstart and layout. Possibly I shouldn't directly call a plugin controller action though, but rather only render partials from the plugin and let the host application handle the main controllers/views.
These are the viewstart locations that are tried in my own VirtualPathProvider:
"~/Plugins/MyMvcApplication.dll/_ViewStart.cshtml"
"~/Plugins/MyMvcApplication.dll/_ViewStart.vbhtml"
"~/Plugins/_ViewStart.cshtml"
"~/Plugins/_ViewStart.vbhtml"
"~/_ViewStart.cshtml"
"~/_ViewStart.vbhtml"
I'm also considering just forgetting about embedding views as it seems a bit fiddly, so I might just opt for copying views to the host application, which could make debugging easier for future users. The risk is that the users will make changes to these views, making updates of the plugin harder. One of the reasons I have doubts about the plugin mechanism is because of possible performance implications, though I haven't done any measurements yet.

Is it possible to modify the output of the razor viewengine just before sending it to the client?

Im building an ASP.NET MVC 3 app using Razor as template language.
Here is what I would like to do:
When all template content from cshtml-files for a certain request have been parsed in razor viewengine and ready to output to the visitor - then I would like to insert some extra information into the parsed html content.
So my question is:
Is there an event of any kind to hook on to inside the Razor viewengine or inside the MVC framework that allows me to do this kind of changes to the output?
Don't ask why I'd want to do something like this in a MVC application, it's a long and boring story.
Given the vagueness of your question, perhaps this article might help, or maybe you can specify in what way the article isn't helpful...
Dependency Injection in ASP.Net MVC Views
UPDATE:
How about an ActionFilter? I seem to recall that you can get the viewresult and tinker with it in an ActionFilter, although I have only ever done this for a json transformation. Here's another article: Use ASP.Net action filters to render
Here's a SO answer--this might be a duplicate question!

Resources