Here’s a really simple way to toggle actions or more information to keep your pages clean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I’m asked quite a bit how to bind a dropdown list in ASP.NET MVC. So there are about 10,000 ways to do it, but here’s one.
First, we’ll create a simple interface to return a list of SelectListItem so we can use the built-in DropDownListFor() later on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Then we can create a SelectListProviderAttribute to take in the Type of list provider we want. You can see in this example I'm instantiating the object with StructureMap...you could do this with Activator or something else.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now we just implement an ISelectListProvider and add the attribute to our model like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[SelectListProvider(typeof(CommunitySelectListProvider))] [DisplayName("Community")] public int CommunityId { get; set; }
Now we just call our new HtmlHelper in our view and we're done. So here's the helper:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
var property = expression.GetProperty();
IEnumerable<SelectListItem> selectList;
var selectListProviderAttribute = property.GetAttribute<SelectListProviderAttribute>();
if (selectListProviderAttribute != null)
{
var provider = selectListProviderAttribute.Provider;
selectList = provider.Provide();
}
else
{
throw new ArgumentException(string.Format("SelectListProvider not specified for property \"{0}\"", property.Name));
I'm sure you all realized that I used some additional helpers in code samples above. So for the curious few, I uploaded the full sample project to github. Browse the source now