0

I have some div in my page which contains some input elements. Now I want to clone complete html of this div so that I can save it in my database and next time I can retrieve that cloned html and again render it in another page in the exact same way as it was at the time of saving including all input values same as they were at the time of saving.

I am cloing the html of div as:

 var htmlObject= $('.mydiv').clone();

now above variable htmlObject is of type object.

Now in c# in my model I've string type of property Html:

public string Html { get; set; }

But when I send htmlObject to my server using ajax call, the Html property is null. My question is what should be the type of property Html so that the htmlOject is catched properly with all its properties preserved so that next time when I retrieve it from db and send it to client the div should be recreated exactly same?

Saurabh Palatkar
  • 3,242
  • 9
  • 48
  • 107
  • how do you pass that htmlObject back to the server? – Nilesh Nov 13 '14 at 11:06
  • Also try using `htmlObject.html()` to the server as suggested in the answer by @Jarek – Nilesh Nov 13 '14 at 11:13
  • As I am using angular js, using angularjs resource to make the ajax request. Simply passing the htmlObject in ajax request. the htmlObject which I am passing is of type oject. Which contain all the dom related info. – Saurabh Palatkar Nov 13 '14 at 11:17
  • $.html() doesn't preserves the input values thats why I am using $.clone(), otherwise output of $.html() I can bind directly to the string property in server. – Saurabh Palatkar Nov 13 '14 at 11:19
  • `$("input").each(function () { $(this).attr("value", $(this).val()); });` just add this before you clone. – Nilesh Nov 13 '14 at 11:31

1 Answers1

0

Instead of using clone(), which returns a DOM object, grab the string contents of the div using:

var htmlString = $('.mydiv').html();

You can then pass the htmlString directly to the C# string property Html.

Edit: to include input values in the string, see include new input values when selecting from the dom using jquery

Community
  • 1
  • 1
  • $.html() doesn't preserves the input elements values. thats why I am using $.clone(). – Saurabh Palatkar Nov 13 '14 at 11:13
  • Ah. C# won't recognize a DOM object (at least out of the box), you have to convert the output of `clone()` into a string on Javascript side. I've added a link to a question about this that has an answer that you might be able to use. – Jarek Piórkowski Nov 13 '14 at 11:25