I need to create an HtmlHelperextension to render an Autocomplete. Something like this:
@Html.AutoCompleteFor(x => x.CustomerId);
The problem is that the Html.RenderPartial(...) returns void, so my AutoCompleteFor method must also be void. But Razor won’t let it compile because @Html.X will only compile if X returns an Object.
I know I can bypass this problem by calling this, instead:
@{ Html.AutoCompleteFor(x => x.CustomerId); }
But that will make my code to look inconsistent with the @Html.EditorFor
I need to return a PartialView from inside my an HtmlHelper
Some considerations:
- I know you might think that this would be, somehow, breaking the MVC pattern, but MVC itself does that. The
Html.EditorForwill try to find a view and return it. - I’m only trying to do that because I need
foreign-keyproperty to usejQuery-AutoCompleteby default. I wasn’t able to tell MVC to use my template for foreign-key properties.
You should call
Html.Partial, which returns aHelperResultobject rather than writing directly to the page.You can then return the
HelperResultto the caller.Technically, you could also just
return null, but that would be a really dumb idea.