Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC partial views: input name prefixes
    text
    copied!<p>Suppose I have ViewModel like</p> <pre><code>public class AnotherViewModel { public string Name { get; set; } } public class MyViewModel { public string Name { get; set; } public AnotherViewModel Child { get; set; } public AnotherViewModel Child2 { get; set; } } </code></pre> <p>In the view I can render a partial with </p> <pre><code>&lt;% Html.RenderPartial("AnotherViewModelControl", Model.Child) %&gt; </code></pre> <p>In the partial I'll do</p> <pre><code>&lt;%= Html.TextBox("Name", Model.Name) %&gt; or &lt;%= Html.TextBoxFor(x =&gt; x.Name) %&gt; </code></pre> <p>However, the problem is that both will render name="Name" while I need to have name="Child.Name" in order for model binder to work properly. Or, name="Child2.Name" when I render the second property using the same partial view.</p> <p>How do I make my partial view automatically recognize the required prefix? I can pass it as a parameter but this is too inconvenient. This is even worse when I want for example to render it recursively. Is there a way to render partial views with a prefix, or, even better, with automatic reconition of the calling lambda expression so that </p> <pre><code>&lt;% Html.RenderPartial("AnotherViewModelControl", Model.Child) %&gt; </code></pre> <p>will automatically add correct "Child." prefix to the generated name/id strings?</p> <p>I can accept any solution, including 3-rd party view engines and libraries - I actually use Spark View Engine (I "solve" the problem using its macros) and MvcContrib, but did not find a solution there. XForms, InputBuilder, MVC v2 - any tool/insight that provide this functionality will be great.</p> <p>Currently I think about coding this myself but it seems like a waste of time, I can't believe this trivial stuff is not implemented already.</p> <p>A lot of manual solutions may exists, and all of them are welcome. For example, I can force my partials to be based off IPartialViewModel&lt;T&gt; { public string Prefix; T Model; }. But I'd rather prefer some existing/approved solution.</p> <p>UPDATE: there's a similar question with no answer <a href="https://stackoverflow.com/questions/955371/partial-view-with-parametrized-prefix-for-controls-names">here</a>.</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