Question

How do I get the drop down to display as part of my editor template?

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 thanks to this sample.

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.

My EditorTemlates\User.cshtml (dynamically generating the layout based on ViewData):

<table style="width: 100%;">
@{
    int i = 0;  
    int numOfColumns = 3;

    foreach (var prop in ViewData.ModelMetadata.Properties
        .Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) 
    { 
        if (prop.HideSurroundingHtml) 
        { 
            @Html.Display(prop.PropertyName) 
        }
        else 
        { 
            if (i % numOfColumns == 0)
            {

                @Html.Raw("<tr>");
            }

            <td class="editor-label">
                @Html.Label(prop.PropertyName)
            </td>
            <td class="editor-field">
                @Html.Editor(prop.PropertyName)
                <span class="error">@Html.ValidationMessage(prop.PropertyName,"*")</span>
            </td>

            if (i % numOfColumns == numOfColumns - 1)
            {
                @Html.Raw("</tr>");
            }
            i++;
        }
    }
}
</table>

On the View I'm then binding the SelectList seperately, and I want to do it as part of the template.

My Model:

public class SecurityEditModel
{
    [ScaffoldColumn(false)]
    public SelectList roleList { get; set; }

    public User currentUser { get; set; }
}

My Controller:

public ViewResult Edit(int id)
{
    User user = repository.Users.FirstOrDefault(c => c.ID == id);

    var viewModel = new SecurityEditModel
        {
            currentUser = user,
            roleList = new SelectList(repository.Roles.Where(r => r.Enabled == true).ToList(), "ID", "RoleName")
        };

    return View(viewModel);
}

My View:

@model Nina.WebUI.Models.SecurityEditModel

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>


@using(Html.BeginForm("Edit", "Security"))
{
    @Html.EditorFor(m => m.currentUser)
    <table style="width: 100%;">
        <tr>
            <td class="editor-label">
                User Role:
            </td>
            <td class="editor-field">
                <!-- I want to move this to the EditorTemplate -->
                @Html.DropDownListFor(model => model.currentUser.RoleID, Model.roleList)
            </td>
        </tr>
    </table>
    <div class="editor-row">
        <div class="editor-label">

        </div>
        <div class="editor-field">

        </div>
    </div>
    <div class="editor-row">&nbsp;</div>
    <div style="text-align: center;">
        <input type="submit" value="Save"/>&nbsp;&nbsp;
        <input type="button" value="Cancel" onclick="location.href='@Url.Action("List", "Clients")'"/>
    </div>

}       

Hopefully that's clear enough, let me know if you could use more clarification. Thanks in advance!

Was it helpful?

Solution

Since you need access to the SelectList you can either create an editor template bound to SecurityEditModel or you can pass the SelectList in ViewData. Personally I would go with the strongly typed approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top