mvc telerik网格中的网格操作

时间:2011-05-28 06:23:30

标签: asp.net asp.net-mvc

如何在网格中执行编辑和删除操作

我有以下网格

<%=Html.Telerik().Grid(Model).Name("Grid").Columns(columns =>
    {

        columns.Bound(m => m.Keywords);
        columns.Bound(m => m.Country).Title("Location");
        columns.Bound(m => m.AreaID);
        columns.Bound(m => m.JobSearchAgentID).Hidden(false);

    }).DataBinding(databinding =>
        {
            databinding.Server().Select("Agentlist", "Grid", new
            {
                ajax = ViewData["ajax"]
            });
            databinding.Ajax().Select("Agentlist",
               "Grid").Enabled((bool)ViewData["ajax"]);
        })
               .DataKeys(keys =>
                   {
                       keys.Add(m => m.JobSearchAgentID);
                   }
                   )
        .Scrollable(scrolling => scrolling.Enabled((bool)ViewData["scrolling"]))
        .Sortable(sorting => sorting.Enabled((bool)ViewData["sorting"]))
        .Pageable(paging => paging.Enabled((bool)ViewData["paging"]))
        .Filterable(filtering => filtering.Enabled((bool)ViewData["filtering"]))
        .Groupable(grouping => grouping.Enabled((bool)ViewData["grouping"]))
        .Footer((bool)ViewData["showFooter"])


           %>
      <%}%>

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

以下是允许在网格中添加和编辑的网格示例:

<% Html.Telerik().Grid<ReportingPeriodGroupDto>()
    .Name("ReportingPeriodGroupAdminGrid")
    .DataKeys(keys => keys.Add(o => o.Id))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .ToolBar(commands =>
    {
        commands.Insert();
    })
    .DataBinding(dataBinding => dataBinding.Ajax()
            .Select("SelectReportingPeriodGroup", "Admin")
            .Insert("InsertReportingPeriodGroup", "Admin")
            .Update("UpdateReportingPeriodGroup", "Admin")
    )
    .Columns(columns =>
    {
        columns.Bound(o => o.ShortDescription).Width("10em").Width("8em");
        columns.Bound(o => o.LongDescription).Width("20em");
        columns.Command(commands => commands.Edit()).Title("Actions");
    })
    .Footer(false)
    .Render();
%>

注意:您必须将这些插入和更新方法添加到控制器

[AcceptVerbs(HttpVerbs.Post)]
[GridAction(GridName = "ReportingPeriodGroupAdminGrid")]
public ActionResult InsertReportingPeriodGroup()
{
    ReportingPeriodGroupDto reportingPeriodGroupDto = new ReportingPeriodGroupDto();
    TryUpdateModel(reportingPeriodGroupDto);
    if (ModelState.IsValid)
    {
        reportingPeriodGroupDto.CreatedBy = UserId;
        reportingPeriodGroupDto.CreatedDate = DateTime.Now.ToString();
        ITransformer transformer = ServiceFinder.Instance.ServiceFactory.RedPortalTransformerFactory.GetTransformer(reportingPeriodGroupDto.GetType());
        ReportingPeriodGroup parent = (ReportingPeriodGroup)transformer.Transform(reportingPeriodGroupDto);
        RedPortalDbContext.ReportingPeriodGroups.Add(parent);
        RedPortalDbContext.SaveChanges();
    }
    return SelectReportingPeriodGroup();
}

[AcceptVerbs(HttpVerbs.Post)]
[GridAction(GridName = "ReportingPeriodGroupAdminGrid")]
public ActionResult UpdateReportingPeriodGroup()
{
    ReportingPeriodGroupDto reportingPeriodGroupDto = new ReportingPeriodGroupDto();
    if (TryUpdateModel(reportingPeriodGroupDto))
    {
        reportingPeriodGroupDto.UpdatedBy = UserId;
        reportingPeriodGroupDto.UpdatedDate = DateTime.Now.ToString();
        ITransformer transformer = ServiceFinder.Instance.ServiceFactory.RedPortalTransformerFactory.GetTransformer(reportingPeriodGroupDto.GetType());
        ReportingPeriodGroup parent = (ReportingPeriodGroup)transformer.Transform(reportingPeriodGroupDto);
        RedPortalDbContext.ReportingPeriodGroups.Add(parent);
        RedPortalDbContext.Entry(parent).State = EntityState.Modified;
        RedPortalDbContext.SaveChanges();
    }
    return SelectReportingPeriodGroup();
}