Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting EditorTemplate into HtmlHelper
    text
    copied!<p>I originally created an editor template like this</p> <pre><code>@model MyModel var items = // get items @Html.DropDownListFor(m =&gt; m.Id, items, new { id = Html.IdFor(m =&gt; m) }) </code></pre> <p>which was invoked by</p> <pre><code>@Html.EditorFor(m =&gt; m.SomeClass) </code></pre> <p>where <code>SomeClass</code> has an <code>Id</code> property. (The <code>IdFor</code> is one of my HTML helpers).</p> <p>This would generate something like this</p> <pre><code>&lt;select name="SomeClass.Id" id="SomeClass" /&gt; </code></pre> <p>Now I want to change the editor template into an HTML helper, so that my call looks like this</p> <pre><code>@Html.CustomEditorFor(m =&gt; m.SomeClass) </code></pre> <p>I'm changing this from a view to helper because its easier for reusability.</p> <p>This is loosely what I have:</p> <pre><code>public static MvcHtmlString CustomEditorFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { var idString = htmlHelper.IdFor(expression); var propertyValue = expression.Compile()(htmlHelper.ViewData.Model); var items = // get items return htmlHelper.DropDownListFor(expression, items, new {id = idString}); } </code></pre> <p>However, when I call the editor, I get this HTML instead of what I want.</p> <pre><code>&lt;select name="SomeClass" id="SomeClass" /&gt; </code></pre> <p>How can I modify the expression enough to allow it to "access" the <code>Id</code> property?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload