Timsen
Timsen

Reputation: 4126

MVC 3 telerik grid edition mode

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

Upvotes: 0

Views: 2610

Answers (2)

Lester
Lester

Reputation: 4413

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.

Upvotes: 2

Brian Mains
Brian Mains

Reputation: 50728

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

Upvotes: 0

Related Questions