I’m trying to refine my HTML extension method, so that I can bind both the ID and Value components to work something like this:
@Html.AutoCompleteFor(model => model.Customer.ID, model => model.Customer.Name, "/Customer/Autocomplete")
I currently do it like this:
@Html.AutoCompleteFor(model => model.CustomerID, model => model.CustomerID_display, "/Customer/Autocomplete")
Where I need to extend the model to include CustomerID_display, which is a bit clunky, and requires very specific post processing.
The reason I need to bind on the display value (which is the text name of the entity), is so that if the user enters a new item, it can be detected and potentially auto-generated.
My expected method above doesn’t work of course, and I might be way off the mark. But you should know effectively what I’m trying to do. If my expected method above worked, my implementation of AutoCompleteFor would be quite simple (everyone is using the word trivial these days!). I’m expecting that lambda can be a bit more useful, perhaps:
@Html.AutoCompleteFor(model => new AutoCompleteBinding { ID = model.Customer.ID, Name = model => model.Customer.Name }, "/Customer/Autocomplete")
Thanks!
In your HTML “…For” extensions, you normally only have a single Expression parameter.
Simply expand this, to include another expression. Adding:
To make:
You can now use expression2, as you would normally with expression. (Of course you should name it something intuitive).
Usage will be as described in the question: