Custom HtmlHelper with EditorFor Template
I have an EF model that I'm passing into the views to Create and Edit. I have a field that is a string (varchar(100)) that is meant to store a delimited list of potential values. That list should be populated by a checkboxlist. I know how to get my hardcoded list to display, but I'm trying to figure out how to handle the EditorFor template so that I can convert the checked values to a delimited string.
For example, I may have a series of checkboxes in my HtmlHelper that contain fruit (ie: Apple, Banana, Cherry, Strawberry). If two of them are chosen, I want those 开发者_高级运维values (ie: Apple,Strawberry) to be passed to the method on the controller to be saved in the database.
Any ideas how I may be able to accomplish this.
There are two possible ways I would do this. Either name your checkboxes in a way that the values will be sent to the server as an array and then build the delimited string there. This obviously wouldn't be preferred as you might then have to use this logic in many places.
The other way is to have some javascript in the template that builds your delimited string as checkboxes are clicked and puts it into a hidden field which is what get's submitted to your action method.
http://jsfiddle.net/infernalbadger/Xjjf2/1/
精彩评论