I'm looking for a sample of how to generate a dropdownlist from a model in vb.net mvc 3.
It seems almost everything is written in C# but I have to do it in vb.NET
I was trying this:
Private _Q1 As IEnumerable(Of SelectListItem)
Public Property Question1() As IEnumerable(Of SelectListItem)
Get
Return _Q1
End Get
Set(ByVal value As IEnumerable(Of SelectListItem))
_Q1 = value
End Set
End Property
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "-Lựa chọn-", Value = "Selects items" });
var cat = (from c in _database.SubCategories where c.Categories.CategoryId==1 select c).ToArray();
for (int i = 0; i < cat.Length; i++)
{
list.Add(new SelectListItem { Text = cat[i].SubCategoryName, Value = cat[i].SubCategoryId.ToString(), Selected = (cat[i].SubCategoryId == article.SubCategoryId) });
}
ViewData["danhsach"] = list;
In View:
<td>@Html.DropDownList("SubCategoryId", (IEnumerable<SelectListItem>)ViewData["danhsach"],
new { id = "SubCategoryId" })</td>
You can reference and apply to your vb code Thanks for reading my post
Come and enjoy the beauty of Paracel and Spratly Islands in Việt Nam
Trần Lê Thành Trung
mcoolin
Member
2 Points
13 Posts
dropdownlist
Jun 19, 2012 06:38 PM|LINK
I'm looking for a sample of how to generate a dropdownlist from a model in vb.net mvc 3.
It seems almost everything is written in C# but I have to do it in vb.NET
I was trying this:
Private _Q1 As IEnumerable(Of SelectListItem)
Public Property Question1() As IEnumerable(Of SelectListItem)
Get
Return _Q1
End Get
Set(ByVal value As IEnumerable(Of SelectListItem))
_Q1 = value
End Set
End Property
but it is ignored.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: dropdownlist
Jun 19, 2012 06:55 PM|LINK
Use a code converter: http://www.codechanger.com
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
bkis1112
Participant
1061 Points
226 Posts
Re: dropdownlist
Jun 20, 2012 02:25 AM|LINK
I'm have a sample code in C#:
In Controller:
List<SelectListItem> list = new List<SelectListItem>(); list.Add(new SelectListItem { Text = "-Lựa chọn-", Value = "Selects items" }); var cat = (from c in _database.SubCategories where c.Categories.CategoryId==1 select c).ToArray(); for (int i = 0; i < cat.Length; i++) { list.Add(new SelectListItem { Text = cat[i].SubCategoryName, Value = cat[i].SubCategoryId.ToString(), Selected = (cat[i].SubCategoryId == article.SubCategoryId) }); } ViewData["danhsach"] = list;In View:
<td>@Html.DropDownList("SubCategoryId", (IEnumerable<SelectListItem>)ViewData["danhsach"], new { id = "SubCategoryId" })</td>You can reference and apply to your vb code Thanks for reading my post
Trần Lê Thành Trung
mcoolin
Member
2 Points
13 Posts
Re: dropdownlist
Jun 20, 2012 11:55 AM|LINK
Should this code not be in your model?
I'm trying to get this to work from the model without the database.
mcoolin
Member
2 Points
13 Posts
Re: dropdownlist
Jun 20, 2012 11:59 AM|LINK
Well thanks for that:
The code it produced was:
Public Property Products() As IEnumerable(Of SelectListItem)
Get
End Get
Set(value As IEnumerable(Of SelectListItem))
End Set
End Property
But it does not work...
Allen Li - M...
Star
10411 Points
1196 Posts
Re: dropdownlist
Jun 21, 2012 07:18 AM|LINK
Hi, please refer to the replies on the following link:
http://forums.asp.net/t/1741846.aspx/1
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework