2014-10-09 150 views
0

我尝试添加动态使用jQuery添加动态使用jQuery

此代码,我成功地写了创建下拉列表中可以根据需要,但是代码渲染意外格式的HTML标签,在这里它把DropDownList的帮手MVC DROPDOWNLIST HTML助手每个属性值的双引号! (如姓名=“” HousingUnitCount“”)

<select class="&quot;form-control" input-sm&quot;="" id="&quot;HousingUnitCount&quot;" name="&quot;HousingUnitCount&quot;"><option value="&quot;&quot;">Choose the number of housing Unit</option> 
<option value="&quot;0&quot;">0</option> 
<option value="&quot;1&quot;">1</option> 
<option value="&quot;2&quot;">2</option> 
<option value="&quot;3&quot;">3</option> 
<option value="&quot;4&quot;">4</option> 
<option value="&quot;5&quot;">5</option> 
</select> 

下面是代码我有,

这里我定义为我的列表和HTML辅助全局变量jQuery的

@{ 
ViewBag.Title = "TestingDynamicFormWithjQuery"; 

var unitsCountList = new List<SelectListItem> { new SelectListItem() {Text="0", Value="0"}, 
               new SelectListItem() {Text="1", Value="1"},  
               new SelectListItem() {Text="2", Value="2"}, 
               new SelectListItem() {Text="3", Value="3"}, 
               new SelectListItem() {Text="4", Value="4"}, 
               new SelectListItem() {Text="5", Value="5"} 
               }; 

var housingUnit = Html.DropDownList("HousingUnitCount", unitsCountList, "Choose the number of housing Unit", htmlAttributes: new { @class = "form-control input-sm" }).ToHtmlString(); 
} 
使用

这是我使用

<button id="addUnitBtn" type="button" name="addUnitBtn" class="btn btn-primary"> 
    <span class="glyphicon glyphicon-plus-sign"></span> Add Unit 
</button> 

@using(Html.BeginForm()) 
{ 

    <div id="addUnitDiv"> 

    </div> 

    <br/><br /><br /> 
    <input id="Submit" type="submit" value="Save" class="btn btn-primary btn-lg" /> 
} 

下面是jQuery的代码中添加的DropDownList dynamicall的HTML Ÿ

@section Scripts{ 
<script type="text/javascript"> 
    $(window).ready(function() { 
     console.log('windows ready'); 
     $('button[name="addUnitBtn"]').on("click", function() { 
      console.log('btn clicked'); 
      $("#addUnitDiv").append('<div>@Ajax.JavaScriptStringEncode(housingUnit)</div>'); 
     }); 
    }); 
</script> 
} 
+1

'JavaScriptStringEncode'将编码字符串。你需要对它进行编码吗? – luke2012 2014-10-09 21:37:14

+0

那么我怎样才能在JavaScript代码中嵌入一个HTML助手而不使用JavaScriptStringEncode?我试图嵌入mvc帮手而不使用JavaScriptStringEncode,它不起作用:( – SaDHacK 2014-10-09 22:37:02

回答

2

这个怎么样

<script type="text/html" id="template"> 
     <div class="template-wrapper"> 
      @Html.DropDownList("HousingUnitCount", unitsCountList, "Choose the number of housing Unit", htmlAttributes: new { @class = "form-control input-sm"}) 
     </div> 

    </script> 

然后在你的JavaScript代码:

@section Scripts{ 
<script type="text/javascript"> 
    $(window).ready(function() { 
     console.log('windows ready'); 
     $('button[name="addUnitBtn"]').on("click", function() { 
      console.log('btn clicked'); 

      var $template = $.trim($($('#template').html())); 

      updateTemplateElementsNameAttr($template); 

      $("#addUnitDiv").append($template); 
     }); 
    }); 

function updateTemplateElementsNameAttr($template){ 
    var nextIndex = $('.template-wrapper').length; 

    $template.find('select').each(function(){ 
    var nameAttr = $(this).attr('name') + '[' + nextIndex + ']'; 
    $(this).attr('name', nameAttr); 

    } 
} 
</script> 
} 
+0

这是一个gr8的答案,它确实为我工作,但是使用脚本模板并不能让我选择具有动态生成的attribut名称(例如dynamiclly在数组([0],[1] ...)下拉列表名称前缀以允许绑定到ICollection) – SaDHacK 2014-10-10 21:57:39

+1

为此,您需要使用jquery更新每个模板的名称属性,我更新了我的答案。 – sanjeev 2014-10-11 14:11:27