DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
221 stars 78 forks source link

Can the options for DropDownListFor come from ViewBag? #393

Closed profnimrod closed 9 years ago

profnimrod commented 9 years ago

I have a List in my ViewBag. It contains many items so I'd rather not include it in the Model as I don't want it posted back to the server when the form is submitted. I have the following:

@f.FormGroup().DropDownListFor(x => x.Guid, ViewBag.ListItems).Label().LabelText("Generic Type")

The error I get says:

An anonymous function or method group cannot be used as a constituent value of a dynamically bound operation.

I'm assuming this is referring to the fact that the ViewBag is a dynamic type. Is there a way to make this work without having to include my ListItems in the view's model? I want the selected value to populate x.Guid.

DmitryEfimenko commented 9 years ago

The whole list of options is not going to be posted back to the server on submit. You can easily verify this in Chrome Dev tools in the network tab.

However, if you still want to use ViewBag, just specify its type:

@f.FormGroup().DropDownListFor(x => x.Guid, (IEnumerable<SelectListItem>)ViewBag.ListItems).Label().LabelText("Generic Type")
profnimrod commented 9 years ago

Many thanks for your rapid response as always Dmitry. I was so close to solving it just as you posted. I should have given it a little more time before troubling you. Apols.

Thanks also for the note about the whole list being posted back to the server. My mistake. Using ViewBag or ViewData just happens to be convenient in this case.

DmitryEfimenko commented 9 years ago

no problem, good luck!