2012-02-15 30 views
20
@Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles)) 

Đoạn mã trên tạo cho tôi danh sách lựa chọn tốt. Nhưng tôi muốn thực hiện lựa chọn tùy chọn. Rất tiếc, không có tùy chọn trống và tôi muốn thêm một tùy chọn. Tôi sẽ làm như thế nào?@ Html.DropDownListFor Cách thêm tùy chọn?

+0

Xem hướng dẫn DDL tôi http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and- jquery/using-the-dropdownlist-helper-với-aspnet-mvc và http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc. aspx – RickAndMSFT

Trả lời

44

Bằng việc sử dụng proper DropDownListFor overload:

@Html.DropDownListFor(
    model => model.ZipFile, 
    new SelectList(ViewBag.ZipFiles), 
    "-- please select a zip file --" 
) 
+0

Ahh ok hoàn hảo, cảm ơn! –

+0

Xem hướng dẫn DDL của tôi http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc và http : //blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT

+0

Tuyệt vời, cảm ơn! – Lee

1

Trong bộ điều khiển, khi bạn đặt ViewBag.ZipFiles, hãy thêm một SelectListItem vào bộ sưu tập đó.

+0

Có một cách dễ dàng hơn. –

+0

Vâng tôi đoán điều đó cũng có tác dụng. Thậm chí không nghĩ về nó. –

+0

Đẹp - Tôi thích phương pháp của bạn. –

6
@Html.DropDownListFor(model => model.Country, new List<SelectListItem> 
    { 
    new SelectListItem { Text = "India", Value = "1"}, 
    new SelectListItem { Text = "USA", Value = "2"}, 
    new SelectListItem { Text = "Sreelanka", Value = "3"}, 
    new SelectListItem {Text = "Africa",Value="4"}, 
    new SelectListItem { Text = "China", Value = "5" }, 
    new SelectListItem { Text = "Austraila", Value = "6" }, 
    new SelectListItem { Text = "UK", Value = "7" } 
    }, "Select Country", 
    new {@Style = "Width:500px;height:40px;", 
    @class = "form-control input-lg"}) 
Các vấn đề liên quan