Insert partial view into layout retrieved from database - c#

I am storing user-editable HTML layouts inside a database, but I would like to know if there's a way to render a partial view in the middle of some of that content.
For example:
<div>
This is the header
#Html.Action("WebsiteTemplate", "Page")
</div>
I've thought that I could split the top & lower content into 2 separate columns, but is there a better way to accomplish this?

What you need is to parse with Razor content which you retrieved from database. You may use RazorEngine project for that purpose. It has some limitations (like, #Raw() instead of #Html.Raw()), but cover many scenarios.

Related

How to generate a .pdf file by keeping the Bootstrap 4 layout using C# asp.net?

I'm looking for a solution, in C# Asp.net, allowing me to generate a .pdf file of detailed statistics (of a Partialview I created) while keeping the layout of Bootstrap as well and my personal CSS too. I tried to use Dinktopdf but apparently this library does not support Bootstrap 4 flexboxes and I end up with a totally distorted statistics table
I expect this kind of result
But the actual output is
As you can see, the right column content seems to be out of document.
This is true that there is no wacky CSS for this document but I would like to reuse it on another document with more CSS styles.
If any of you would have a solution to this problem or would have another bookstore (instead of DinkToPdf) to suggest me.
I have used this recently and found that the Bootstrap grid system worked well for this type of layout. here is what i used to get a split 2 column layout.
<div class='col-xs-6'>
"add content here"
</div>
<div class='col-xs-6'>
</div>
"add content here"
<div class='clearfix'></div>
the "clear fix" just ensures the next column div you add goes back to first column.
i also found that the HTML parsing of DinkToPDF is not great, make sure there are no extra spaces next <,>,= seems to confuse it.

Display Paragraph with same format(With line Break)

I store two paragraphs in sql server in same field. My Problem is while retrieving data from database and display in the report it display both paragraphs as single Paragraph. But while using TextArea it displays good. but Is there any options to display Paragraphs as i stored in HTML
It would be great if you provide what tag you have been using or just a code sample of your current project.
But this sounds like the paragraph is being displayed inline, possibly from span , i or no tags at all.
If you have no wrappers, wrap it inside <div> Quack! </div> or <p> Quack!</p>.
If you have existing wrappers and would like to keep them, set their css styling into display: block.
I guess this is the best I could answer without a slight clue of what the sample is.
Or if you are injecting results from the server, this would mean a different case and you will need to loop through the results and wrap each of them.

C# MVC modify View HTML before rendering on browser

I was wondering if it is possible to modify a view HTML before sending it to the browser.
I wanted to create a custom tag compiler where i can insert a simple tag as <my-parsing-tag></my-parsing-tag> on the view and replace it for some specific HTML.
I'm already using OnActionExecuting and OnActionExecuted filters to execute some actions on the context (Change ViewBags, View names, Sessions, etc.), i also tried to do it there but i couldn't find the correct place to get the HTML, well i don't even know if it's possible to do so.
Is it possible or i would need to store my views HTML on the database to accomplish what i need ?
EDIT
As #Juan asked, why i need it:
I'm working with a call to action system where the user can place some specific modal campaigns on the page he wants just using those simple tags or selecting the page that will display it.
After that i will append the selected HTML to the view before sending it to the user. This system is intended for users that can't work editing the views since they don't work with HTML.
EDIT 2
After some research i have tried to implement a custom RazorView, the code is here with the Index View HTML, but now i have two problems:
The first one is that my Index View has some HTML that is coming from the database and is placed there using vars on my ViewModel and instead of the call to action HTML being placed at the end of my Index View, it's being placed before the ViewModel vars. The second problem is that the HTML is being duplicated instead of replaced. Here is an image of how the result looks like:
http://imgur.com/a/elul1
You could use an HtmlHelper extension for this:
http://tech.trailmax.info/2012/08/creating-custom-html-helper-in-mvc3/
I would suggest the following:
Define a container in your template (layout most likely) that will receive any content the user decides to "drop" into it via the admin panel.
You let the view know there is something to display via the ViewBag.
The view uses information you passed in order to render the desired content.
How it renders is where the HTMLHelper extensions come in. You could create an extension method which renders partial views based on the information you pass to it or maybe a set of extension methods that you call selectively based on the desired widget.

Simple MVC Question for MVC beginner

i have an Ajax ActionLink which normally just returns a PartialView (which is just a UserControl ascx file) however, my needs have changed and i want to return another PartialView (so a total of two PartialViews) that occupy different areas of my page... of course i can't call " return PartialView("UserControl.ascx") " twice in a row consecutivelly... so my question is what would be an elegant work around for this?
how can i return two PartialViews WITHOUT wrapping these two PartialViews up in a larger parent view? i hesitate to do this because both items are in a different part of the html table which would require me to include practically the entire page in the parent view due to the structure of the table, and in this case lots of html data unnecessarily would be sent to the browser at each request - defeating the purpose of an ajax call/partial update (correct me if i'm wrong).
Im sorry I misread, i thought you wanted to call an action method statically. If you want to update 2 parts with one click then I dont really now how you would do it with the included apis. What you could do is create a little javascript (jquery!) that takes over the link´s click, and then have the script load the render page with ajax.
I´ll post an example in a few minutes :P
<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#TheLink").click(){
$("#PlaceToUpdate1").load("/Controller/Method/View1");
$("#PlaceToUpdat2").load("/Controller/Method/View2");
}
});
});
</script>
im not sure if that will work exactly like that (no compiler, just top of my head) but its something like that. Of course the link should be a dummy link that doesnt do anything, since the script is the one actually doing it (though you can intercept the links methods if you send back a false or something like that)
You could manually construct the partial views in HTML helper methods. However, the feasibility of that approach depends on whether or not the partial views will be reused in other pages. I build a data grid control from scratch using HTML helper methods. Some of my pages have three or four of these data grids managed by a jQuery accordion control to reduce the screen footprint. I chose building an HTML helper for a number of reasons. First, I expected to use this control throughout my applications. Second, I didn't want to burden my views with a bunch of conditional logic. Finally, I wanted to be able to change the grid's configuration (including the model) within the view, so I wouldn't have to recompile every time I changed it. The grid supports both LINQ-to-SQL models and user-defined classes (using reflection), has a built-in pager control and a search mechanism that supports multiple search fields in a grid. I also set up the columns so that they can either display formatted text, link to a controller action or hold a mailto: link. Within the grid itself, you can define the model to populate the grid, optionally set the columns to display, specify the action and controller for creates, and specify a JavaScript function for deletes (because I use the jQuery dialog plugin for confirmation messages). All these changes are managed in the view itself.
Learning how to leverage HTML helper methods gives you the closest thing to ASP.NET server controls that MVC provides.

what is the easiest way to retain values of fields while switching from page to page

I am using Multiview. And I am switching between views. Each view contains lots of fields. I am going to another view from the current view to add some data. And after adding data from the new view, I am returning to the previous view. Now on this view I want to populate fields which I have entered before switching.
Currently I am using ViewState to retain previous values. But this costs lot as there are lots of fields on a single view. Is there any other way to do this task?
This isn't too far off from what viewstate is designed for--I'd stick with that.
Other less-desirable alternatives include sessions, database tables, and httpcontext.
You can store those values in a temporary table otherwise i dont think there is any problem with Viewstate.. better make a structure, store your values in that structure and store the structure in one viewstates...
Another possiblity is to use the Server.Transfer("url", true) which allows you to pass the previous form along with the data that was contained in the form to the next page.
Check out http://www.developer.com/net/asp/article.php/3299641 for usage examples.

Categories

Resources