C# 基于razor pages模型中的多对多关系选择TreeView值
我有一个Razor pages项目,它试图从使用数据模型创建的数据库中填充剑道树视图(或任何其他树视图) 我正在处理的页面包含正在使用的应用程序,树正在读取自引用组织列表,以便我们知道每个组织或部门等可以访问哪些应用程序。 我正在使用razor应用程序中的编辑页面,因此~Pages\Apps\Edit.cshtml和~Pages\Apps\Edit.cshtml.cs与关联的模型页面如下所示。 这些是涉及的模型,忽略此问题的RoleApps:C# 基于razor pages模型中的多对多关系选择TreeView值,c#,razor,treeview,kendo-treeview,C#,Razor,Treeview,Kendo Treeview,我有一个Razor pages项目,它试图从使用数据模型创建的数据库中填充剑道树视图(或任何其他树视图) 我正在处理的页面包含正在使用的应用程序,树正在读取自引用组织列表,以便我们知道每个组织或部门等可以访问哪些应用程序。 我正在使用razor应用程序中的编辑页面,因此~Pages\Apps\Edit.cshtml和~Pages\Apps\Edit.cshtml.cs与关联的模型页面如下所示。 这些是涉及的模型,忽略此问题的RoleApps: namespace FliveRetry.Model
namespace FliveRetry.Models
{
public class Org
{
public int ID { get; set; }
public string OrgName { get; set; }
public int? ParentID { get; set; }
public bool? HasChildren { get; set; }
}
}
及
第二个是DropDownTreeview:
@(Html.Kendo().DropDownTree()
.Placeholder("Select ...")
.Name("selectedOrgs")
.DataTextField("OrgName")
.DataValueField("ID")
.Checkboxes(checkboxes => checkboxes
.Name("ischecked")
.CheckChildren(true))
.AutoClose(false)
.Value(Model.selectedOrgNames)
.HtmlAttributes(new { style = "width:100%" })
.DataSource(d => d
.Read(read =>
read.Url("/Apps/Edit?handler=Read")
)
)
)
这两个示例都从edit.cshtml.cs中读取数据源:
public async Task<IActionResult> OnGetAsync(int? id)
{
this.TreeData = GetOrgTreeData();
if (id == null)
{
return NotFound();
}
App = await _context.App
.Include(i => i.OrgAppJoins).ThenInclude(i => i.Org)
.Include(i => i.RoleAppJoins).ThenInclude(i => i.Role)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.ID == id);
if (App == null)
{
return NotFound();
}
PopulateAssignedAppRoleData(_context, App);
PopulateAssignedAppOrgData(_context, App);
return Page();
}
public IActionResult OnGetRead(int? id)
{
var result = from e in _context.Org
where id.HasValue ? e.ParentID == id : e.ParentID == null
select new
{
id = e.ID,
hasChildren = (from q in _context.Org
where (q.ParentID == e.ID)
select q
).Count() > 0,
OrgName = e.OrgName,
selected = (from s in _context.OrgAppJoin
where (s.OrgID == e.ID) && (s.AppID == 2)// <--this works, this doesn't--> (s.AppID == app.ID)
select s
).Count() > 0,
ischecked = (from s in _context.OrgAppJoin
where (s.OrgID == e.ID) && (s.AppID == 2)// <--this doesn't work, this doesn't either-->
(s.AppID == app.ID)
select s
).Count() > 0
};
return new JsonResult(result);
}
public IActionResult OnGetRead(int?id)
{
var result=来自_context.Org中的e
其中id.HasValue?e.ParentID==id:e.ParentID==null
选择新的
{
id=e.id,
hasChildren=(来自_context.Org中的q)
其中(q.ParentID==e.ID)
选择q
).Count()>0,
OrgName=e.OrgName,
已选择=(来自_context.OrgAppJoin中的s)
其中(s.OrgID==e.ID)&(s.AppID==2)//您是否有正确显示的树的示例?您有cshtml,基本上您正在尝试使用cshtml转换数据库结果。cshtml是JSON,因此您正在尝试将数据库模型序列化为JSON。正确吗?与其发布所有代码,不如试着将问题简化为序列化b时遇到的问题y发布您当前拥有的结果和您实际需要的结果。这两个树都正确显示树,dropdowntree和treeview,都不选中它们应该选中的框,dropdowntree不读取但正确保存数据,treeview既不保存也不读取,但它显示OnGetRead中的选定属性为不同彩色背景等根据选定的剑道默认设置,您首先需要使复选框正常工作。这似乎是错误的。复选框(复选框=>复选框。名称(“ischecked”)。复选子项(true))语句中应该有一个等号设置为True或False。
public async Task<IActionResult> OnGetAsync(int? id)
{
this.TreeData = GetOrgTreeData();
if (id == null)
{
return NotFound();
}
App = await _context.App
.Include(i => i.OrgAppJoins).ThenInclude(i => i.Org)
.Include(i => i.RoleAppJoins).ThenInclude(i => i.Role)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.ID == id);
if (App == null)
{
return NotFound();
}
PopulateAssignedAppRoleData(_context, App);
PopulateAssignedAppOrgData(_context, App);
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id, string[] selectedOrgs, string[] selectedRoles)
{
if (!ModelState.IsValid)
{
return Page();
}
var appToUpdate = await _context.App
.Include(i => i.OrgAppJoins).ThenInclude(i => i.Org)
.Include(i => i.RoleAppJoins).ThenInclude(i => i.Role)
.FirstOrDefaultAsync(s => s.ID == id);
if (await TryUpdateModelAsync<App>(
appToUpdate,
"app", // Prefix for form value.
c => c.AppName, c => c.AppDescription, c => c.DisplayOrder))
{
UpdateAppOrgs(_context, selectedOrgs, appToUpdate);
UpdateAppRoles(_context, selectedRoles, appToUpdate);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
UpdateAppOrgs(_context, selectedOrgs, appToUpdate);
UpdateAppRoles(_context, selectedRoles, appToUpdate);
PopulateAssignedAppOrgData(_context, App);
PopulateAssignedAppRoleData(_context, App);
return Page();
}
@(Html.Kendo().TreeView()
.Name("selectedOrgNames")
.DataTextField("OrgName")
.Checkboxes(checkboxes => checkboxes
.Name("ischecked")
.CheckChildren(true))
.HtmlAttributes(new { style = "width:100%" })
.DataSource(d => d
.Read(read =>
read.Url("/Apps/Edit?handler=Read")
)
)
)
@(Html.Kendo().DropDownTree()
.Placeholder("Select ...")
.Name("selectedOrgs")
.DataTextField("OrgName")
.DataValueField("ID")
.Checkboxes(checkboxes => checkboxes
.Name("ischecked")
.CheckChildren(true))
.AutoClose(false)
.Value(Model.selectedOrgNames)
.HtmlAttributes(new { style = "width:100%" })
.DataSource(d => d
.Read(read =>
read.Url("/Apps/Edit?handler=Read")
)
)
)
public IActionResult OnGetRead(int? id)
{
var result = from e in _context.Org
where id.HasValue ? e.ParentID == id : e.ParentID == null
select new
{
id = e.ID,
hasChildren = (from q in _context.Org
where (q.ParentID == e.ID)
select q
).Count() > 0,
OrgName = e.OrgName,
selected = (from s in _context.OrgAppJoin
where (s.OrgID == e.ID) && (s.AppID == 2)// <--this works, this doesn't--> (s.AppID == app.ID)
select s
).Count() > 0,
ischecked = (from s in _context.OrgAppJoin
where (s.OrgID == e.ID) && (s.AppID == 2)// <--this doesn't work, this doesn't either-->
(s.AppID == app.ID)
select s
).Count() > 0
};
return new JsonResult(result);
}