I would like to know how can I generate a DropDownList using an UIHint attribute. I already customized some of the predefined attributes but I don't know how to proceed for generating DropDownLists.
Here is how I did with my last one and I want to use it in a similar way:
public class CartProduct
{
[Required]
[UIHint("Spinner")]
public int? Quantity { get; set; }
[Required]
[UIHint("MultilineText")]
public string Description { get; set; }
}
Here's an (untested) general example using generics. There's probably a simpler way of achieving the same thing.
Model:
DropDownListModel class:
Controller:
_DropDownList.cshtml editor template:
And, finally, your view:
This gives you a generic
DropDownListModel
that you can use anywhere, with any type. UseEditorFor
andUIHint
to specify the editor template and reuse the view all over the place.