Is it possible to "pipe" output from one view engine into another? - asp.net

I was wondering if it is possible (even if it is a long shot) to pipe generated markup from one view engine into another with ASP.NET?
Could a view engine be built to explicitly to support such a thing (given the constraint of the ASP.NET Framework)?
What about Web Forms and Razor?

If you want to include output from one view (Razor) into the another could you create it as a partial view and call render partial in the other.
Even it your other view is not partial you can create some kind of "adapter partial view" that will call #Html.RenderAction() or #Html.RenderPartial() in order to include the first view.

Related

Returning custom data along with actionresult from an action method

I am sure many people have had this question but I failed to get any results after searching the web and SO or the search keywords were different.
I am working on a new asp.net mvc web app where I get a plain template returned by the index action method on the controller. Later in the document.ready event handler I build the ui dynamically and append the dom elements to the blank template and this just works fine. My issue is I need 2 server calls here,
1) to get the view from the index action method
2) an ajax call inside the document.ready{} to get the data using which I build the ui.
I was wondering if there is any method using which I could pass back the data from the index action method along with the blank template view and use this data to create the ui inside the document.ready event handler. This will save that one additional hit to the server.
The reason for not using partial views is
1) we have some functionality already developed in jquery and
2) in my org people think making the functionality using razor and partial view will not be as flexible, for example building and raising customevent in js is a great feature that helps to keep the functionality loosely coupled from other features. (please correct if we are wrong)
Edit: I thought an example will explain this better,
Say I need to create a list of users, but the entire list and its functionality like checkboxes selection etc are built by a js module. So along with the blank view i want to pass the "users" object which is a class in my models currently.
Kindly suggest.
You have a couple of options:
1) Server-side rendering:
Putting the necessary data into the model would seem to be the obvious thing to do...that's what MVC models are for.
During the building of the HTML your View code runs - so you can access the model values in Razor code, which you can use to build your view and influence the final HTML. So in this scenario you build your view using Razor, rather than constructing it using JS code. You can of course still use JS to change it after the page has loaded, but it would be downloaded into the browser with the HTML already in the desired starting state.
2) Client-side rendering, but with necessary data pre-populated:
If you'd rather stick with your existing client-side rendering code, you could use Razor to inject some ready-made JSON into the JavaScript, so it's effectively hard-coded into the page when it first runs, rather than having to fetch it from the server separately via AJAX.
For example, if you have some object in C# which holds the data, you can serialise it to a JSON string and then use Razor to write that string into your JS in the correct place.

Custom HtmlHelper for multiple MVC apps

I am currently working on multiple ASP.NET MVC web apps.
All of these web apps have the same navigation bars/menus.
Some of the menu items are app specific, so they can be passed from the respective app.
Some of the menu items are not app-specific, such as whether user is admin or not, based on which I show an admin link on the nav bar. The logic for getting the admin property is available in the business layer.
Is it possible to make this html helper such that I don't have to pass the non-app specific parameters from the respective apps ?
Can I call the business layer from the html helper ?
Is it advisable ?
I want this html helper or any other solution easily distributable...
Thanks
HTML helpers are extension methods on the System.Web.Mvc.HtmlHelper type that return an System.Web.Mvc.MvcHtmlString object. If you want "easily distributable", then you can create a library project with the helper extensions that you need. Then add the project in as a reference on the MVC project.
#using statements can bring in the extensions to the Razor view. The helper object that you bring in through the extension method in the library will give you access to most of the information available to the Razor view at the time the helper is called (with the ViewContext property).
The extensions can be overloaded as much as needed to account for variations in the projects. Common menu options can be added to the library as a static collection that can be accessed by both the MVC project and the extension methods.
update
MVC is set up such that you can do what you want. You have a lot of control. Your helper can include as much code as you need. It's not like you are "breaking the rules". But best practice dictates that you keep your business logic in the controller. By putting that into the helper, which gets called by the Razor view, you are in effect moving the business logic into the Razor view.
HTML Helpers in general are a lightweight way to create HTML code. Thus they are easy to reuse any you can have dozens or hundreds on a single Razor view. That idea gets broken when you move a bunch of business logic into the helper. Then you have a potential of slowing things down if the helper is to be reused a lot.
Good rule of thumb for MVC, if your helper starts getting complicated, create a partial view. I would probably create a model to represent the menu, then create a partial view in the Shared folder that uses that model, then call it from the parent view. I think that would give you more flexibility, and be more in keeping with the MVC best practices.

How to organize partial view in ASP.NET MVC3?

I am trying to figure out how to organize my partial views in my project. Some people say to precede the name of a partial view with an _, but that makes for weirdly named actions in the controller if the view can be called directly.
Also, what should be done if the view can be a partial view in some cases and a regular view in other cases?
A common example for this is a search view that I embed on some pages to search for users in my app, but I also have a search page that loads the same view. I suppose I could create a second view for the search page that just embeds the partial view. Just wondering what other people are doing.
Honestly it's a matter of preference. You should do whatever works in your application with respect to avoiding code (or view) duplication etc.
The reason why we (I'm a dev on the team developing MVC) recommend preceding the partial view filename with an underscore is to more easily distinguish between full and partial views when looking at files in VS
I also use my partials with the underscore character as a prefix to easily distinguish between a view and a partial view when managing the files. As your project becomes bigger you may have a lot of files for a single controller, so this convention will help you a lot.
Besides, when you use a partial view you can call your views with an action using the following:
public ActionResult MyPartialAsAView()
{
// your code
return View("_myPartialView");
}
You have to remember that if you are using your partial as a View, you should assign the layout to it depending on the mode the partial is working (as a view or partial view), for example with a boolean property on your model class.

ASP.NET MVC: Basic form question

A friend has asked me to help him with a project that's MVC. This is my first experience with MVC. I'm trying to build the MVC components for a form for him.
A page has a modal popup which uses a JavaScript to POST or GET and receive HTML backā€”that it displays inside the popup modal.
I need to create an MVC form that has validation to display inside aforementioned popup. This popped-up form will be used elsewhere on the site, so needs to be modular.
Should I create an ActionResult in my Route's Controller that returns an View (.aspx) containing my form? Should I instead create a Partial View (.ascx) that has the form, then use that Partial View in a View for my Route so other parts of the site--other Routes--can do the same?
I'm stuck at that basic understanding. From there, I don't even know what to do about the validation (was told the same validation will be used on a nearly identical form) and how a ViewModel fits into this like is used elsewhere on the site.
I've been reading a lot and looking at a lot of examples but I'm still confused.
You've got two separate questions here really. To deal with whether you should use a partial view or a regular view, just think of it this way. Does the HTML content in question represent a full page, or just a piece of a page that will be reused inside of other pages?
If it's the former, then use a full View. If it's the latter, then put it in a partial view. It sounds to me like you just need a full View. In either case, it's easy to change to the other if it turns out it's not meeting your needs.
As to validation, take a look at xVal if you're using ASP.Net MVC 1. It allows for easy validation using attributes on your models.
When you make your post from the modal popup do the validation then in the actionmethod you created specifically for that popup. If you want client side validation write up the js to do it.
As for the modal markup and what not just create a partial view for that, shouldn't be a big deal.

How to make rich/compound views

I have recently started to examine ASP.NET MVC. I have studied quite a few examples and common to these are that they contain quite simple scenarios, where a view will map to either an instance of a type in the model or a list of a paritcular type from the model.
I'm looking for guidelines to compose/composite views. In the long term I would like Ajax to be part of the equation, but for now I'm looking for a simpler non Ajax setup.
The problem
Here is a description of a contrieved problem. A domain model contains the types A and B.
class A
{ String ID, String Name, int Age
List<B> ListOfB
}
class B
{ String ID, String Name, String Url}
I would like to have a that allows the following:
A DropDownList showing type A information
Details about the particular A picked in the dropdown
A list showing type B's related to the selected type A
A form that makes it possible to edit the data of a selected type A
A form that enables the user to add a new B.
The view should not show all of this at once, instead it has to show different combinations of information and functionality according to user input. Combinations of detail and functionality could forexample be:
Initially only show the dropdownlist containing A's
If an A has been selected in the dropdown:
Show the particular A as selected in the dropdown
Show detail info about the selected A
Show list of detail info of related type B's
The user wants to edit a particular A
Show the particular A as selected in the dropdown
Show form that allows user to edit particular A
Show list of detail info of related type B's
The user wants to add a new B
Show the particular A as selected in the dropdown
Show detail info about the selected A
Show list of detail info of related type B's
That could look something like this (used the web version of balsamiq mockups - what a fantastic invention!):
Combination 2:
Combination 4:
Creating the view and controller
Since the solution has to allow for different combinations of data and functionality, I think it would be smart to have a parent view (not to be confused with a masterpage) that contained placeholders for parital views. Then, the parent views controller could make up the right combinations of model data and partial views and feed these to the parent view.
Finally; the questions:
Is my way of thinking in accordance with the asp.net mvc methodology?
Can you explain/show (not necessarily all of it) how the controller can compile the right combination of partial views and feed these to the parent view?
Can you point me towards an Ajax based solution?
Can you suggest books/links that contain examples of complex views?
asp.net mvc fully supports all of your requirements but there are few things you should get up to speed on:
You should look at implementing view models to help seperate your domain model from your specific views. Here is a good link on how to start this.
You need to get up to speed with a client side javascript framework for the ajax work with partial html rendering. jquery will do this or ms ajax. Here is an example
To your detailed questions:
Is my way of thinking in accordance with the asp.net mvc methodology?
Asp.net mvc is not going to constrain you at all so essentially this is fully supported
Can you explain/show (not necessarily all of it) how the controller can compile the right combination of partial views and feed these to the parent view?
You can use partial views if you want to seperate bits of code out and can easily refresh them by loading them independently using ms ajax or jquery. You would have a controller that mapped onto your parent view and can delegate and refresh partial views in ajax calls.
Can you point me towards an Ajax based solution?
jquery will do this or ms ajax. Here is an example
Can you suggest books/links that contain examples of complex views?
This link talks a lot about this.

Resources