asp.net mvc - Using jQuery .html with carriage returns -
i'm working on mvc project. have container div want add partial view to. error because partial view's html has carriage returns.
javascript:
$("#containeroptions").html('@{html.renderpartial("_dropdownoptions", model);}');
partial view:
<div id="divoptions"> <label>i browse by:</label> @using (var dd = html.bootstrap().begin(new dropdown("select option").class("dropdownelement").id("dropdownoption"))) { @dd.header("otpions"); @dd.actionlink("city", "#").class("dropdownelement"); @dd.actionlink("zip", "#").class("dropdownelement"); @dd.actionlink("county", "#").class("dropdownelement"); } </div>
i know html code not issue because works elsewhere. have duplicated issue putting <p>hi</p><p>fred</p>
in partial view, works.
but
<p>hi</p> <p>fred</p>
does not work.
is there way force call .html ignore carriage returns in document? prefer not remove them decreases readability.
edit:
i attempted string replace remove new lines, did not work.
var s = '@{html.renderpartial("_dropdownoptions", model);}' var bob = s.replace(/[\n\r]/g, ''); $("#containeroptions").html(bob);
instead of injecting html string constant javascript code, inject view html hidden element on page, html, , reference that:
<script id="template" type="text/template"> @html.partial("_dropdownoptions", model) </script>
text/template
unknown content type, ignored rendering.
jquery:
// reparents elements $("#containeroptions").append( $('#template').children() );
or
// slower, leaves original intact $("#containeroptions").html( $('#template').html() );
this give better readability , flexibility. html in strings "bad" idea.
Comments
Post a Comment