2014-03-29 27 views
0

我正在開發ASP.NET MVC應用程序。如何從MVC視圖中自動彈出下拉ID?

我有一個下拉不完全下拉,但我給了下拉的感覺。

用戶可以在列表中添加n個產品。

當用戶開始在文本框中輸入時,以輸入字符開頭的產品開始出現在該文本框的下方。看到圖像...

enter image description here

它的做工精細,同時從下拉列表中選擇該項目。

現在,問題出現在我想獲取用戶添加的產品的產品ID時。

我想獲得用戶添加的產品ID數組。

我的控制器代碼

public ActionResult NewvendorList(string term) 
     { 
      var results = new[] 
      { 
      new { id = "1", label = "Suraj Metels" }, 
      new { id = "2", label = "A.B. Infra" }, 
      new { id = "3", label = "Momin Brother" }, 
      new { id = "4", label = "Fort"}, 
      new { id = "5", label = "Malabar Hill" }, 
      new { id = "6", label = "A.B.C. Limited" }, 

      }; 
      var result3 = results.Where(s => s.label.ToLower().StartsWith(term.ToLower())).Select(w => w).ToList();  
      return Json(result3, JsonRequestBehavior.AllowGet); 
     } 

形式查看代碼,用來獲取從控制器方法數據jQuery代碼...

<script type="text/javascript"> 

    $(document).ready(function() { 
     var i = 0; 


     $("#lnkAddProduct").on("click", function() { 
      //$('#ProductId').val(""); 
      @*var url = "@Url.Content("~/Scripts/GeneralScript.js")"; 
       $.getScript(url, function() { 
       });*@ 
      var d = '<div class="addedProduct "><div class="control-group " style="margin-bottom:10px;"><input type="text" class="ProductName span2 pull-left" id="Product_Name_' + i + '" name="Product_Name" value="" placeholder="start typing to load products")"/><input type="hidden" class="ProductCode" name="Product_Code" id="ProductCode_' + i + '" value=/><a href="#" class="clsremove font-midium" style="margin-left:5px;margin-top:5px;" id="remove_' + i + '">remove</a></div></div>'; 
      $('#ProductList').append(d); 
      alert("newID:"+i); 

      $(".clsremove").unbind("click").click(function() { 
       $(this).closest(".addedProduct").remove(); 
      }); 


      $('body').delegate(".ProductName", "focusin", function() { 
       $(this).autocomplete({ 
        source: '@Url.Action("NewvendorList","Product")', 

       minLength: 1, 

       select: function (evt, ui) { 


       $("#Product_Name_"+ i).val(ui.item.label); 


       $("#ProductCode_" + i).val(ui.item.id); 

       alert($("ProId" + $("#ProductCode_" + i).val(ui.item.id))); 


       } 


      }); 
      }); 

      i = i + 1;   
     });  
    }); 


</script> 

如何獲得thise ID的同時保存記錄?

+0

請澄清情況和要求的更多。無法獲得用戶添加的產品的產品ID –

回答

0

這可能是你想要的。

onclick of create按鈕u調用jquery處理程序並獲取div或跨度的值,無論您使用什麼,並將所有這些值放入數組中。然後你可以指定數組的值給部分隱藏的元素,並在控制器端訪問它的提交形式

var ids=[]; 
$('span[id^=ProductCode]').each(function(){ 
    ids.push(parseInt($(this).text())); 
}); 
var st=ids.join(); 
alert(st); 
$('#hdn').val(st); 

FIDDLE