-1

I am new to MVC. I want to get the view html into a string variable that is in my controller. All this should happen at the click of a button that is in that same view.

FadiY
  • 119
  • 2
  • 10
  • 1
    possible duplicate of [Render a view as a string](http://stackoverflow.com/questions/483091/render-a-view-as-a-string) – GSerg Mar 18 '15 at 13:05
  • I have actually seen many different solutions to this but none of those seemed to work for me or maybe I didn't implement them in a good way. I guess I am looking for a "as simple as possible" solution. – FadiY Mar 18 '15 at 13:08
  • The linked question's solution is as simple as possible. I am using it in my app for mailing purposes. – GSerg Mar 18 '15 at 13:10
  • Maybe I should give it a second go. I probably did something wrong. Thanks! – FadiY Mar 18 '15 at 13:13
  • Actually I was wrong, my actual production code was based on http://weblog.west-wind.com/posts/2012/May/30/Rendering-ASPNET-MVC-Views-to-String, as the comment goes. – GSerg Mar 18 '15 at 13:27
  • *face palm* saw that page earlier today... looks like my worst nightmare :D But seriously though, I will have a go at it and let you know when I'm finished in a year or so :) – FadiY Mar 18 '15 at 13:34

1 Answers1

0

Run some Javascript on the button click to capture the innerHTML of the whole page, then pass it to a controller method via Ajax.

The Ajax should look something like:

$.ajax({
            type: "GET", //Or POST,etc
            url: '@Url.Action("MethodName", "ControllerName")',
            data: { $("html").html()},
            success: function (result) {
                 Add some code here if your method returns anything, if not, this is optional.
            },
            error: function (req, status, error) {
                Error catching is nice. The error parameter is the text of the error, so you could do something like
                alert(error);
            }
        });

This, or something very similar, should get you what you need. In the controller name, just put the part of the name before the word controller, so if the method is on HomeController, just put Home as ControllerName.

9Deuce
  • 689
  • 13
  • 27