c# - 带有动态下拉菜单的 MVC 3 编辑器模板

标签 c# visual-studio-2010 asp.net-mvc-3 .net-4.0 mvc-editor-templates

如何让下拉菜单显示为我的编辑器模板的一部分?

所以我有一个用户实体和一个角色实体。角色作为 SelectList 传递给 View ,用户作为用户传递给 View 。 SelectList 变成一个下拉菜单,选择了正确的 ID 和所有内容 thanks to this sample .

我正在尝试使用 MVC 3 为我的实体获得一个完美捆绑的一体式 EditorTemplate,这样我就可以调用 EditorForModel 并在我有外键时添加一个下拉列表来很好地布局字段在此特定实例中,诸如角色之类的东西。

My EditorTemlates\User.cshtml(根据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>

在 View 上,我然后单独绑定(bind) SelectList,我想将它作为模板的一部分。

我的模型:

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

    public User currentUser { get; set; }
}

我的 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);
}

我的看法:

@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>

}       

希望这已经足够清楚了,如果您需要更多说明,请告诉我。提前致谢!

最佳答案

由于您需要访问 SelectList,您可以创建一个绑定(bind)到 SecurityEditModel 的编辑器模板,或者您可以在 ViewData 中传递 SelectList。我个人会选择强类型方法。

关于c# - 带有动态下拉菜单的 MVC 3 编辑器模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8538077/

相关文章:

C# 错误 : Cannot implicitly convert type 'int' to 'short'

c++ - 刷新MFC界面 VC++ 2010

c# 在对象不是 null 时将其报告为 null

c# - 如何使用 MSpec 测试由 ASP.NET MVC 操作设置的 HTTP 状态代码

asp.net-mvc - RenderPartial 问题(将数据发送到母版页时)

asp.net-mvc-3 - MVC3 Razor 和模拟同一页面部分

c# - WPF 使用绑定(bind)分配静态资源

c# - Xamarin Android 应用程序在 Release模式下崩溃(Parse.Android SDK)

c# - 在 C# 中序列化没有 GMT 的日期时间

c# - 在 C# 中设置列​​宽 (Interop.Excel)