i don't understand how i can set the default selected value for a dropdown list. My actual code is:
[DisplayName("avatar")]
public string avatarId { get; set; }
public SelectList avatar
{
get
{
return new SelectList(GetAvatars(), "Name", "Name", avatarId);
}
}
the GetAvatars() function return a List<MyObject> that contains the avatars informations such as filename and file url. I don't know if i this is correct, anyway i want to display a selectbox that shows the list of the avatars in a folder with a predefinited
avatar selected and i would like to show an image near the selectbox with the current selected avatar. How can i do it? I don't find any way to set the default value
It looks like you're using the
correct constructor. If it's not working, are you sure you're setting avatarId before you get
avatar? and that one of the MyObjects in the List<MyObject>s has a "Name" property with the value of
avatarId?
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;" id="_mcePaste">http://msdn.microsoft.com/en-us/library/dd492553.aspx is a
</div>
vecchia
Member
3 Points
60 Posts
DropDownListFor and selected value
Aug 31, 2010 02:46 PM|LINK
Hi all,
i don't understand how i can set the default selected value for a dropdown list. My actual code is:
[DisplayName("avatar")] public string avatarId { get; set; } public SelectList avatar { get { return new SelectList(GetAvatars(), "Name", "Name", avatarId); } }the GetAvatars() function return a List<MyObject> that contains the avatars informations such as filename and file url. I don't know if i this is correct, anyway i want to display a selectbox that shows the list of the avatars in a folder with a predefinited avatar selected and i would like to show an image near the selectbox with the current selected avatar. How can i do it? I don't find any way to set the default value"ASP.NET MVC" ".NET Framework 4.0" <asp.net>
Dirkle
Member
427 Points
118 Posts
Re: DropDownListFor and selected value
Aug 31, 2010 04:43 PM|LINK
It looks like you're using the correct constructor. If it's not working, are you sure you're setting avatarId before you get avatar? and that one of the MyObjects in the List<MyObject>s has a "Name" property with the value of avatarId?
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;" id="_mcePaste">http://msdn.microsoft.com/en-us/library/dd492553.aspx is a</div>
vecchia
Member
3 Points
60 Posts
Re: DropDownListFor and selected value
Aug 31, 2010 04:47 PM|LINK
MyObject is a class like this:
public class MediaModel { public string url { get; set; } public string name { get; set; } }where url is the url of the avatar and name is the filename. In my MVC view i have:
<%= Html.DropDownListFor(model => model.avatarId, Model.avatar, null, new { @class = "text" }) %>but i'm not sure if this is correct
ignatandrei
All-Star
134913 Points
21619 Posts
Moderator
MVP
Re: DropDownListFor and selected value
Aug 31, 2010 06:17 PM|LINK
Please modify
into
return new SelectList(GetAvatars(), "url", "name", avatarId);
if
I suppose that you mean List<MediaModel> instead of List<MyObject>
More, to select in the dropdownlist an item , you should pass the information of the item key on "SelectList"
But you have the MediaModel without avatarid. Please add... and reformulate
return new SelectList(GetAvatars(), "avatarId", "name", avatarId);
After finishing with previous problem, this is resument on javascript/jquery on client.Will see later...
Basically, this is about : what do you want to display in dropdown and what do you want to be the key ?
vecchia
Member
3 Points
60 Posts
Re: DropDownListFor and selected value
Aug 31, 2010 06:22 PM|LINK
i solved setting the avatarId property in the controller
return View(new MyModel() { avatarId = "mykey" }