简体   繁体   中英

Pass xml from controller to js

What I'm going to achive on UI. I have a table of objects and one parameter of the object is xml string. When I click by link for some row there should open pop-up with formatted xml. So, I've tried to pass xml string to View with viewmodel and then put it to data attribute of a link. Read data attribute with js and put it to pop-up. I use bootstrap modal-dialog. So it looks smth like this:

<a class="open-xml" data-toggle="modal" data-xmlstring="@modal.Xml">Open xml</a>

and in js:

$(document).on("click", ".open-xml", function() {
        var responseXml = $(this).data('xmlstring');
        $("#xml-viewer").find("textarea").text(responseXml);
    }
);

but it show only first word. I've tried to parse it to json and encode it in js but it still doesn't work for me. Maybe you have another solution for it, or working example of current?

To set value of textarea use val()

$("#xml-viewer").find("textarea").val(responseXml);

If this isn't working you would probably do better to retrieve the xml as text using ajax and set value using the response text

Solved it. Add another div with displayed none in a link with

@model.Xml
. And by the click get this peace of html and put it to the pop-up with js. So I transfer to js not xml, string or json but formatted html.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM