MVC 3 Telerik 网格编辑模式

发布于 2024-12-29 01:55:18 字数 1766 浏览 1 评论 0原文

根据我的观点之一,我正在使用 Teleriks 开源网格进行 MVC 3。网格中有 3 列:

-Name -电子邮件 -角色

我遇到的问题是:我希望我的角色列作为下拉列表,在页面加载时它已经可以编辑(所以我不需要任何编辑/更新按钮或东西),所以当管理员更改用户的下拉列表角色中的选定项目时将会更新。

知道怎么做吗?

视图

@model IEnumerable<UserViewModel>
    @(Html.Telerik().Grid(Model)
          .Name("Grid").TableHtmlAttributes(new { width="800"})
            .Columns(columns =>
            {
                //if (userIsInWhateverRole){
                //    columns.Template(o => Html.Action(GenerateYourLinkStuffHere));
                //}
                columns.Bound(o => o.Name).Width(150);
                columns.Bound(o => o.Email).Width(120);
                columns.Bound(o => o.Roles).Width(120);
            })
            .Sortable()
            .Scrollable()
            .Groupable()
            .Filterable()
            .Pageable(paging =>
                paging.PageSize(5)
                      )
                          )

ViewModel

public class UserViewModel
{

    public int Id { get; set; }

    [Microsoft.Build.Framework.Required]
    [Display(Name = "User name")]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    public string[] Roles { get; set; }

    public string Email { get; set; }

    public bool Admin { get; set; }

}

控件

public ActionResult ManageRoles()
{
    var users = Membership.GetAllUsers().Cast<MembershipUser>().Select(x=> new UserViewModel{ Name = x.UserName, Email = x.Email, Roles = Roles.GetAllRoles()});

    return View(users);
}

on one of my views i am using teleriks open source grid for MVC 3. i have 3 columns in the grid :

-Name
-Email
-Roles

the problem which ive got is: i want my Roles column as a dropdownlist, which is already editable when page load (so i dont want any Edit/Update buttons or stuff), so when admin change selected item in dropdownlist role of user gonna be updated.

Any idea how to make that?

View

@model IEnumerable<UserViewModel>
    @(Html.Telerik().Grid(Model)
          .Name("Grid").TableHtmlAttributes(new { width="800"})
            .Columns(columns =>
            {
                //if (userIsInWhateverRole){
                //    columns.Template(o => Html.Action(GenerateYourLinkStuffHere));
                //}
                columns.Bound(o => o.Name).Width(150);
                columns.Bound(o => o.Email).Width(120);
                columns.Bound(o => o.Roles).Width(120);
            })
            .Sortable()
            .Scrollable()
            .Groupable()
            .Filterable()
            .Pageable(paging =>
                paging.PageSize(5)
                      )
                          )

ViewModel

public class UserViewModel
{

    public int Id { get; set; }

    [Microsoft.Build.Framework.Required]
    [Display(Name = "User name")]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    public string[] Roles { get; set; }

    public string Email { get; set; }

    public bool Admin { get; set; }

}

Control

public ActionResult ManageRoles()
{
    var users = Membership.GetAllUsers().Cast<MembershipUser>().Select(x=> new UserViewModel{ Name = x.UserName, Email = x.Email, Roles = Roles.GetAllRoles()});

    return View(users);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2025-01-05 01:55:18

您需要使用模板列并将模板定义为下拉列表:

@(Html.Telerik().Grid(Model)
    .Name("Grid").TableHtmlAttributes(new { width="800"})
    .Columns(columns =>
    {
        //if (userIsInWhateverRole){
        //    columns.Template(o => Html.Action(GenerateYourLinkStuffHere));
        //}
        columns.Bound(o => o.Name).Width(150);
        columns.Bound(o => o.Email).Width(120);
        columns.Template (
            @<text>
                @Html.DropDownList("Roles", new SelectList(item.Roles))
            </text>
        ).Width(120);
    })
    .Sortable()
    .Scrollable()
    .Groupable()
    .Filterable()
    .Pageable(paging => paging.PageSize(5))
)

我无法测试代码,但它应该非常接近。

You need to use a template column and define the template as a dropdown list:

@(Html.Telerik().Grid(Model)
    .Name("Grid").TableHtmlAttributes(new { width="800"})
    .Columns(columns =>
    {
        //if (userIsInWhateverRole){
        //    columns.Template(o => Html.Action(GenerateYourLinkStuffHere));
        //}
        columns.Bound(o => o.Name).Width(150);
        columns.Bound(o => o.Email).Width(120);
        columns.Template (
            @<text>
                @Html.DropDownList("Roles", new SelectList(item.Roles))
            </text>
        ).Width(120);
    })
    .Sortable()
    .Scrollable()
    .Groupable()
    .Filterable()
    .Pageable(paging => paging.PageSize(5))
)

I'm not able to test the code but it should be very close.

药祭#氼 2025-01-05 01:55:18

您希望网格中的每个项目都可编辑吗?只需使用呈现文本框的列的模板即可。请参阅此演示: http://demos.telerik.com/aspnet-mvc/grid/templatesserverside

You want every item in the grid to be editable? Just use a template for a column that renders a textbox. See this demo: http://demos.telerik.com/aspnet-mvc/grid/templatesserverside

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文