[英]Returning ViewBag inside @Html.DropDownListFor inside single quotes
我有一个DropDownListFor这样工作:
<div class="form-horizontal" id=CurrencyDataBlock>
@Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })
</div>
我有一个创建容器和字段的javascript函数。 对于此功能,我通过这种方式在单引号之间传递整个容器:
var part1 = '<br><br><hr style="height:1px;border:none;color:#333;background-color:#333;" /><div><input type="button" class="btn btn-default" onclick="ReTextBox(this)" value="Remove"/></div><textarea rows="10" cols="100" id="Text" name="dyntxt" style="width: 550px; height: 125px; margin-left: auto;"></textarea>'
但是,我的问题是,当我尝试将ViewBag(从第一个主意)传递给单引号时。 我正在这样尝试:
var part3 ='<div class="form-horizontal" id=CurrencyDataBlock>@Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })</div>'
我也尝试过:@ ViewBag.Currency
并且,带有:'@ ViewBag.Currency'
什么都没用。 对于第一个选项,它给出错误:“ ReferenceError:未定义DynamicText”。 第二个想法给了我同样的错误。 当我尝试用单引号引起来时,它甚至无法编译,并给我错误:struct System.Char-将字符表示为UTF-16代码单元。 字符文字中的字符过多
最后,我尝试了这种方式:我也这样尝试了( 想法来自):
var part3 ='<div class="form-horizontal" id=CurrencyDataBlock>@Html.DropDownListFor(model => model.Code, ViewBag.Currency.Replace("\\", "\\\\").Replace("'", "\\'") as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })</div>'
这种方式可以编译,但是页面确实可以加载。
有任何想法吗?
我认为这是你应该做的
不要使用单引号,而是使用` 。 我真的不记得它又叫什么了。 这样,您可以编写HTML而不考虑单引号或双引号问题
我也不知道这是不是错字,你在HTML中有id=CurrencyDataBlock
var part3 = `<div class="form-horizontal" id="CurrencyDataBlock">
@Html.DropDownListFor(model => model.headline, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })
</div>`;
var part1 = `<br><br>
<hr style="height:1px;border:none;color:#333;background-color:#333;" />
<div><input type="button" class="btn btn-default" onclick="ReTextBox(this)" value="Remove"/></div>
<textarea rows="10" cols="100" id="Text" name="dyntxt" style="width: 550px; height: 125px; margin-left: auto;"></textarea>`;
另外,当您要传递列表时,请考虑使用@Html.Raw("your list or other variables here")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.