2014-10-09 93 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