在单引号内返回 @Html.DropDownListFor 内的 ViewBag [英] Returning ViewBag inside @Html.DropDownListFor inside single quotes
问题描述
我有一个 DropDownListFor 以这种方式工作:
@Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0col-sm-2" })
我有一个创建容器和字段的 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 ='@Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--选择货币--", 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 代码单元.字符文字中的字符过多
最后,我尝试了这种方式:我也试过这样(idea from):
var part3 ='@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 = `@Html.DropDownListFor(model => model.headline, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0col-sm-2" })`;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")
I have a DropDownListFor working this way:
<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>
I have a javascript function that creates a container and fields. For this function, I pass the whole container between single quotes this way:
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>'
However, and my problem, is when I try to pass a ViewBag (from the first idea) to a single quote. I am trying like this:
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>'
I also tried with:
@ViewBag.Currency
And, with:
'@ViewBag.Currency'
Nothing is working. For the 1st option, it gives the error:
"ReferenceError: DynamicText is not defined". The 2nd idea gives me the same error. When I try to put the single quotes, it does not even compile and gives me the error:
struct System.Char - Represents a character as a UTF-16 code unit. Too many characters in character literal
Finally, I tried this way:
I also tried like this (idea from):
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>'
This way compiles but the pages does load at all.
Any ideas?
解决方案 This is what I think you should do,
Instead of using single quotes, use `. I don't really remember what it is called again. That way you can write your html without thinking of single or double quotes issues
Also I don't know if it was a typo you have id=CurrencyDataBlock
in your HTML
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>`;
Also when you want to pass lists consider using @Html.Raw("your list or other variables here")
这篇关于在单引号内返回 @Html.DropDownListFor 内的 ViewBag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文