So, here is the Html helper I’ve used :
@Html.DropDownList("FK_gestCompte", User.Identity.Name)
this gives this html result :
<select id="FK_gestCompte" name="FK_gestCompte"><option value="">user3</option>
<option value="1">user1</option>
<option value="2">admin</option>
<option value="3">user2</option>
<option value="5">user3</option>
</select>
I don’t get how I can set the first line like this :
<select id="FK_gestCompte" name="FK_gestCompte"><option value="5">user3</option>
The selectlist I’ve made has been created like that inside the controller :
ViewBag.FK_gestCompte = New SelectList(db.my_aspnet_users, "id", "Name")
any idea?
Here is the solution given byParv Sharma :
controller :
Dim ident As Integer = (From a In db.my_aspnet_users
Where a.name = User.Identity.Name
Select a.id).FirstOrDefault()
ViewBag.FK_gestCompte = New SelectList(db.my_aspnet_users, "id", "Name", ident)
view :
@Html.DropDownList("FK_gestCompte")
going by the signature of the method you are using
im assuing that
User.Identity.Nameis an ienumerable ofSelectListItemso when you are filling this collection you can set any item as selected item using the
Selectedproperty of that item.