views:

29

answers:

2

In my Create View:

<script type="text/javascript">
    var tabContent = "<% Html.RenderPartial("ProductEdit", new Web.Model.Product()); %>";
</script>

Unfortunately this seems to break. At least the quotes (") aren't escaped (\"). How could I "inject" the results of RenderPartial into JS?

A: 

Maybe using '<% Html.RenderPartial("ProductEdit", new Web.Model.Product()); %>' will help?

This doesn't help. I don't know why.
randomguy
A: 

Instead of storing it as a variable, you can just put it in a placeholder, like this:

<div id="tabContent" style="display: none;">
  <% Html.RenderPartial("ProductEdit", new Web.Model.Product()); %>
</div>

Then when you want to use it, get it via .innerHTML, like this:

var myVar = document.getElementById('tabContent').innerHTML;

This is useful in other scenarios, if you want to clone it, etc...it all depends how you intend to use it, but I've found this a much more useful approach in most cases.

Nick Craver
Hey! I'm actually allowing the creation of multiple products on the single create page using javascript (so, yes, I'm cloning). I thought about your approach but then I thought that this can't be good for SEO? With this approach, the partial render is always part of the DOM when with JS approach it can be injected into the DOM on demand (ie. when user adds the first product).
randomguy
"Matt Cutts from google does pretty much say that as long as you are not keyword stuffing your hidden text and abusing the system by trying to trick the googlebot, you should generally be fine." from http://stackoverflow.com/questions/514659/does-google-index-pages-with-hidden-divs
randomguy