2011-05-28 55 views
0

如何執行編輯和電網刪除操作電網運行

我有以下電網您需要了解Telerik的MVC網格控制

http://demos.telerik.com/aspnet-mvc/grid/editingajax

<%=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"]) 


      %> 
     <%}%> 

回答

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