Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Editor Template with Dynamic Drop Down
    primarykey
    data
    text
    <p>How do I get the drop down to display as part of my editor template?</p> <p>So I have a Users entity and a Roles entity. The Roles are passed to the view as a SelectList and User as, well, a User. The SelectList becomes a drop down with the correct ID selected and everything <a href="http://www.nickriggs.com/posts/rendering-and-binding-drop-down-lists-using-asp-net-mvc-2-editorfor/">thanks to this sample</a>. </p> <p>I'm trying to get an all-in-one nicely bundled EditorTemplate for my entities using MVC 3 so that I can just call EditorForModel and get the fields laid out nicely with a drop down added whenever I have a foreign key for things like Roles, in this particular instance.</p> <p>My EditorTemlates\User.cshtml (dynamically generating the layout based on ViewData):</p> <pre><code>&lt;table style="width: 100%;"&gt; @{ int i = 0; int numOfColumns = 3; foreach (var prop in ViewData.ModelMetadata.Properties .Where(pm =&gt; pm.ShowForDisplay &amp;&amp; !ViewData.TemplateInfo.Visited(pm))) { if (prop.HideSurroundingHtml) { @Html.Display(prop.PropertyName) } else { if (i % numOfColumns == 0) { @Html.Raw("&lt;tr&gt;"); } &lt;td class="editor-label"&gt; @Html.Label(prop.PropertyName) &lt;/td&gt; &lt;td class="editor-field"&gt; @Html.Editor(prop.PropertyName) &lt;span class="error"&gt;@Html.ValidationMessage(prop.PropertyName,"*")&lt;/span&gt; &lt;/td&gt; if (i % numOfColumns == numOfColumns - 1) { @Html.Raw("&lt;/tr&gt;"); } i++; } } } &lt;/table&gt; </code></pre> <p>On the View I'm then binding the SelectList seperately, and I want to do it as part of the template.</p> <p>My Model:</p> <pre><code>public class SecurityEditModel { [ScaffoldColumn(false)] public SelectList roleList { get; set; } public User currentUser { get; set; } } </code></pre> <p>My Controller:</p> <pre><code>public ViewResult Edit(int id) { User user = repository.Users.FirstOrDefault(c =&gt; c.ID == id); var viewModel = new SecurityEditModel { currentUser = user, roleList = new SelectList(repository.Roles.Where(r =&gt; r.Enabled == true).ToList(), "ID", "RoleName") }; return View(viewModel); } </code></pre> <p>My View:</p> <pre><code>@model Nina.WebUI.Models.SecurityEditModel @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Edit&lt;/h2&gt; @using(Html.BeginForm("Edit", "Security")) { @Html.EditorFor(m =&gt; m.currentUser) &lt;table style="width: 100%;"&gt; &lt;tr&gt; &lt;td class="editor-label"&gt; User Role: &lt;/td&gt; &lt;td class="editor-field"&gt; &lt;!-- I want to move this to the EditorTemplate --&gt; @Html.DropDownListFor(model =&gt; model.currentUser.RoleID, Model.roleList) &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div class="editor-row"&gt; &lt;div class="editor-label"&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="editor-row"&gt;&amp;nbsp;&lt;/div&gt; &lt;div style="text-align: center;"&gt; &lt;input type="submit" value="Save"/&gt;&amp;nbsp;&amp;nbsp; &lt;input type="button" value="Cancel" onclick="location.href='@Url.Action("List", "Clients")'"/&gt; &lt;/div&gt; } </code></pre> <p>Hopefully that's clear enough, let me know if you could use more clarification. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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