Send MVC actionresult to printer - c#

I have a controller with an action:
SomeController/ActionToBePrinted
ActionToBePrinted() returns an html view.
This action is called from a normal mvc razor view when pressing a button - how would I go about sending the content of the view to a printer when the button is pressed?
Aloha,
Hugo

You cant send direct to the printer.
I suggest you to create a custom ActionResult, that returns a PDF file or something like that. ASP.NET MVC Action Results and PDF Content
You can show a html page as well and open the print dialog using javascript like this
Click to Print This Page
But always the user has to start the print process, you cant do this programmatically.

You can perform a GET request (e.g. use window.open() and pass in URL or use AJAX) and put the returned HTML contents into a new window. Then use
Window.print(). Then simply close the window when you are done.
You could tie this directly into a single view by adding something in the body, but I prefer to use JavaScript in these cases. This keeps the design acting as a re-useable object or service that can be used across multiple views. In other words, you setup the controller-model, but no view. Instead, JavaScript steps in as the View.
Keep in mind that HTML is not a print format. So if you need to control the layout, you should be using a print technology such as PDF. XSLT provides an excellent means to create both HTML and PDF output using the same data, albeit it's a lot more work to create XSLT templates than it is to slap down window.print
Personally, I have an MVC page acting as a service that takes URL parameters. The page hooks into Adobe XSL-FO and uses the params to drive the output.

Related

Split html to show CrystalQuartz graph separately

I built a project (ASP.Net Core) to show in an animated graph the timing of some data extractors I have, where each data extractor has a CronExpression to define that timing for running it.
The framework I'm using to build/show that graph is CrystalQuartz, where I just need (can) is to create the jobs and triggers that will run, add them to the scheduler and call the controller /quartz. What happens beyond the point of calling /quartz (loading pages, scripts and css) is not on my control, and there starts my "issue".
I want to load the graph in just part of the HTML (like the bottom half), where on the top half I would put some links/buttons to change/reload the jobs that are running on the graph without reloading the whole page.
The challenge here is that, anyway I try to call/load the result of calling /quartz, it loads the graph on the whole page.
The question: Is there a way of really splitting the html in two (not using PartialView, doesn't work) where I control the content of html B by the same controller I control html A?
Found the solution.
The way for having other html inside my html is using iframe.
To load the result of a controller inside that iframe, just need to set the attribute src="[route]" (in my case src="/quartz").
Because of the fact the data used on the graph is all loaded on backend, I only needed to call the controller that reload that data and them reload the iframe ([iframe].src = [iframe].src).

Get HTML without serving a view

Im working on a .NET Core Web Application.
I need to creata a PDF from HTML. My idea is to use a controller and view to make the HTML and then convert it to PDF.
How do i call the controller and make it return the HTML inside the project instead of serving it.
The html wont be sent to a client. Only used to create an HTML. Maybe a controller/view is not the way to go?

Post a serialized ASP.NET Model and a file with single Jquery Ajax call in ASP.NET MVC

I am developing a movie library application in asp.net. In admin page, I need to add a movie to the database(details such as name, actors, poster url). And also I need to upload the trailer of the movie to a specific folder in the server.
In my AddMovie View(which is strongly typed to a MovieDetails Model), I have a forms to include movie information as text and with a file input to upload a file(movie trailer). Within my controller I get the binded MovieDetails model and the uploaded file seperately as two parameters as follows.
At first I just posted from my view to this controller without using ajax. I was able to get the Model and the file to the controller to do what I needed to do.
Then I moved to ajax and tried to do this without page refresh.
Now no files cannot be uploaded. I tried to debug and noticed that uploadFile parameter of the controller is null although I attached a file to the file input in the view.
Is there any way to post the model to the controller and upload the file without completely changing the controller and using a single ajax call?
Or else is it possible pass the file path through a model attribute, access that file path and upload the relevant file in the controller, using Ajax.
Thank you!
As Darin mentioned, there is no simple way to handle file-uploading matters via ajax for some security reasons and you can't upload files to your server easily; but good news is there are some tricks which can resolve it, for instance, you can use jquery AjaxForm in your mvc form easily with a bit little modification. Take a look at jquery-ajax-form and its samples, you will get some good idea around it.
Normally you cannot upload files using AJAX. Modern browsers that support FormData would allow you to directly make AJAX requests containing multipart/form-data content. Also look at the native XMLHttpRequest object.
But if you need to support legacy browsers you will need some fallback mechanism such as hidden iframes or Flash movies. Plugins such as blueimp would detect the browser capabilities and upload the file using the best option that the browser supports.

How to extract the extra content loaded in a web page

How to extract the extra content loaded in a web page, which will not be visible in view page source. The extra content is being loaded using ajax. This data can be seen under NET tab using firebug. How to extract this data using c# code.
Two ways :
1- You can use webbrowser to load the same page and get the active document.
2- You can replicate the ajax call made, and use that to get the extra bits that are appended to the document.
And reading your linkedin example above:
When you select the checkbox a ajax call is made , which brings back results and populates the table.You can see that call using firebug console window and see the post parameter and replicate them to get the same result.
Depends on your application in the first place, if you are using c# application as the client for reading a web page, then the the ajax content may not be visible until you put in a javascript engine.
if you are serving the said pages, you only have to log the request response of the server.
More specific question would be appreciated
That extra content is dynamically generated by ajax (for eg: Gridview is generated as table), it is stored in browser's memory. and can be viewed by client side debugging tools (IE has developer tools option).
Once you do a post back, all the control's values are available for C#.
If you are saying extra content, can you please clarify what exactly you are trying to extract using c#?

How to get this file prompted to the user?

Hi Im working on a MVC3 Razor project and I have been stuck on this problem for a few hours now...
I'm trying to get a html to pdf converter to serve a document to the user...
What I want is the following..
A page is rendered and displayed. On the bottom of the page there should be a little icon display something like download as pdf. and what that does is where my problem lies..
All the data that I want is dynamically created within a $("#content").html();
So what I have tried is a jquery/ajax function passing $("#content").html(); as a paramater to my function which creates the pdf (works but I have no clue how to prompt the created file to the user?)
Other solution was #(Html.ActionLink()) but I dont know how to pass the data ($("#content").html()) within that link?
And when trying to work with the functionallity to use the converter to go to the url was a dead end cuz it got its own session and got redirected to the loginpage..
Any help would be appriciated!
I am not familiar with JavaScript, but what if you save the PDF in the temp file first by calling you web service method, and then after it completed you can use JavaScript to navigate to the URL where generated PDF will be returned as content.
Since the XmlHTTPRequest Object cannot handle datatypes other than html, text, json, jsonp and xml you will need to redirect to the pdf location.
I'm not sure what exactly you're doing in your Ajax Request, but once that is completed you can just redirect the window(Form-Action) to the location of the created PDF. This wouldn't actually redirect the browser but only prompt for saving the file.
Why just create a function that returns the created PDF (during ajax request) as File result and then set the window's location to point to this action once the ajax request is completed successfully?
Edit: That means you are not saving the PDF anywhere. So the workaround, is to either use this jquery download plugin or append an iframe dynamically and then post the data through it. Hope this helps.
Figured out a workaround, what I did was write an extension to my HtmlHelper and use that to render my control into a html string instead of a view. Therefor I could use a actionlink to say render this page and get all the html data that way.
http://msug.vn.ua/Posts/Details/3301
Thanks for help tho!

Categories