我在Javascript中有一段代码,其中包含dropdownlist
的定义。
我不知道如何传递像dropdownlist
的名称这样的参数。
var x=1;
var block='<div class="col-md-3">' +
'<div class="form-group">' +
'<label for="Address">Adresa '+ addressOUPIndex+' de la OUP</label><br />' +
'@Ajax.JavaScriptStringEncode(Html.DropDownList("addresses["+ (x- 1) + "].Address",EnumHelper.GetSelectList(typeof(EPURE.Common.Enums.AdresaOUP)), null, new { @class = "selectpicker Address"}).ToHtmlString())' +
^^^^^^^^^^^
'</div>' +
'</div>' +
我收到以下错误:
当前上下文中不存在名称“x”
答案 0 :(得分:1)
尝试替换你的字符串:
var replacedVariable = 1; // unique name inside your 'myString' for replace
var myString = '@Ajax.JavaScriptStringEncode(Html.DropDownList("addresses[replacedVariable].Address",EnumHelper.GetSelectList(typeof(EPURE.Common.Enums.AdresaOUP)), null, new { @class = "selectpicker Address"}).ToHtmlString())';
myString = myString.replace('replacedVariable', replacedVariable); // addresses[1].Address
var block='<div class="col-md-3">' +
'<div class="form-group">' +
'<label for="Address">Adresa '+ addressOUPIndex+' de la OUP</label><br />' +
myString +
'</div>' +
'</div>'