EditorExtensions.EditorFor<TModel, TValue> Method (HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, Object)
Isn't this great, they tell you how to pass some additional data. Now if only someone would tell you how to get at that data in the editor. Not too worry, my pain is your gain.
Additional Data should be passed into the editor using an anonymous type say something like
Html.EditorFor( model => model.Claim , new { employers = Model.Employers, claimStatuses = Model.ClaimStatuses } )
You then access that data in the Editor using
But wait there is more, this Values is actually the ValueCollection that was created by the Dictionary that was created by the anonymous type. That unfortunately means that you can't get to your items using the keys you specified in the anon type, i.e. you CAN'T do ViewData.Values["employers"].
And to make matters worse, it is an ICollection, so you can't do the indexer (it doesn't know how many items are in it) so you CAN'T do ViewData.Values[0].
So how do you get to one exact item in the collection? Well you have to force the ICollection into a list, then you can index it. so using the above example let's assume that the Model.Employers is a collection of SelectListItems, you would get it out doing something like this.
var employers = this.ViewData.Values.ToList( )[0] as IEnumerable<SelectListItem>;
Posted on
17 Apr 10 08:49
by
matthew.hintzen |
Bookmark this post with:
E-mail |
Comments(0) |
Comment RSS