2013-02-12 69 views
0

我有一個選擇列表像一個下拉列表剃刀,而不是<select>與HTML

<select id="select-product"> 
      <option>Select a Product</option> 
    </select> 

我填寫使用Ajax在運行這個名單,現在我想將其轉換爲一個Razor視圖和我會在重定向到頁面之前將數據包含在Viewbag中。

我該怎麼做?

回答

0
$(document).ready(function() { 
    $.get('/Home/GetProducts/' + $(this).val(), function (response) { 
     var products = $.evalJSON(response); 
     var ddlSelectedProduct = $("#SelectedProduct"); 

     // clear all previous options 
     $("#SelectedProduct > option").remove(); 

     // populate the products 
     for (i = 0; i < products.length; i++) { 
      ddlSelectedProduct.append($("<option />").val(products[i].Value).text(products[i].Text)); 
     } 
    }); 
}); 

或者

@Html.DropDownListFor(
    x => x.SelectedProductId, 
    new SelectList(ViewBag.Products as SelectList, "Value", "Text"), 
    "-- Select Product--" 
)