设置树类型以使用用户ID并构建其个人族谱 [英] Setting up a Tree type to utilize a ID for User and build their personal family tree
问题描述
我正在尝试为使用MVC 4的成员构建一个家谱树,我有会员和注册工作,博客和搜索工作我很难建立个人树我的用户。我已经建了两个表,Genealogy和Tree。这些表格的代码如下:
家谱表:
CREATE TABLE [dbo]。[Genealogy](
[GenealogyId] INT IDENTITY(1,1)NOT NULL,
[ FamilyName] NVARCHAR(100)NOT NULL,
[FirstName] NVARCHAR(100)NULL,
[MiddleName1] NVARCHAR(100)NULL,
[MiddleName2] NVARCHAR(100)NULL,
[MiddleName3] NVARCHAR(100)NULL,
[性别] NVARCHAR(10)NULL,
[DOB] DATETIME NULL,
[COOB] NVARCHAR(100)NULL,
[SOB] NVARCHAR(100)NULL,
[COB] NVARCHAR(100)NULL,
[新闻稿] NVARCHAR(10)NULL,
CONSTRAINT [PK_Genealogy] PRIMARY KEY CLUSTERED([GenealogyId] ASC)
);
树桌:
CREATE TABLE [dbo]。[Tree](
[TreeId] INT IDENTITY(1,1)NOT NULL,
[GenealogyId] INT NULL,
[RecordId] INT NULL,
[UserId] NVARCHAR(128)NULL,
[用户名] NVARCHAR(MAX)NULL,
[FamilyName] NVARCHAR(100)NULL,
[FirstName] NVARCHAR(100)NULL,
[MiddleName1] NVARCHAR(100)NULL,
[DOB] DATETIME NULL,
[Count] INT NULL,
[DateCreated] DATETIME NULL,
CONSTRAINT [PK_Tree] PRIMARY KEY CLUSTERED([TreeId] ASC),
CONSTRAINT [FK_Tree_ToGenealogy] FOREIGN KEY([GenealogyId])REFERENCES [dbo]。[Genealogy]([GenealogyId]),
CONSTRAINT [FK_Tree_ToAspNetUsers] FOREIGN KEY([UserId])REFERENCES [dbo] 。[AspNetUsers]([Id])
);
树表与AspNetUsers表和族谱表有关系。我还有Controllers,GenealogyController和TreeBuilderController。
GenealogyController:
使用System;
使用System.Collections.Generic;
使用System.Data;
使用System .Data.Entity;
使用System.Linq;
使用System.Net;
使用System.Web;
使用System.Web.Mvc;
使用WebGenealogy.Models;
命名空间WebGenealogy.Controllers
{
公共类GenealogyController:Controller
{
private GenealogyContext db = new GenealogyContext();
// GET:/ Genealogy /
public ActionResult Index()
{
return view(db.Genealogy。 ToList());
}
// GET:/ Genealogy / Details / 5
public ActionResult细节(int?id)
{
if(id == null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
} b / b $ b / b家谱谱系= db.Genealogy.Find(id);
if(genealogy == null)
{
返回HttpNotFound();
}
返回查看(家谱);
}
// GET:/ Genealogy / Create
public ActionResult Create()
{
return View();
}
// POST:/ Genealogy / Create
//为了防止过度攻击,请启用要绑定的特定属性,对于
//更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost ]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include =GenealogyId,FamilyName,FirstName,MiddleName1,MiddleName2,MiddleName3,Gender,DOB,COOB,SOB,COB,Newsletter)]家谱家谱)
{
if(ModelState.IsValid)
{
db.Genealogy.Add(genealogy);
db .SaveChanges();
返回RedirectToAction(索引);
}
返回查看(家谱) );
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
TreeController:
使用System;
使用System.Collections.Generic;
us使用System.Web获得System.Linq;
;使用System.Web.Mvc获得
;
使用WebGenealogy.Models;
命名空间WebGenealogy.Controllers
{
公共类TreeBuilderController:控制器
{
private GenealogyContext db = new GenealogyContext();
// GET:/ TreeBuilder /
public ActionResult Index()
{
var Tree = TreeBuilder.GetTree(this.HttpContext);
//设置我们的ViewModel
var viewModel = new TreeBuilderViewModel
{
TreeItems = Tree.GetTreeItems(),
};
//返回视图
返回查看(viewModel);
}
public ActionResult AddToCart(int id)
{
//从数据库中检索相册
var addedGenealogy = db.Genealogy
.Single(Genealogy => ; Genealogy.GenealogyId == id);
//将其添加到购物车
var Tree = TreeBuilder.GetTree(this.HttpContext );
Tree.AddToTree(addedGenealogy);
//回到主商店页面更多购物
返回RedirectToAction(索引);
}
[ChildActionOnly]
public ActionResult TreeSummary()
{
var Tree = TreeBuilder.GetTree(this.HttpContext);
ViewData [TreeCount] = Tree.GetCount();
返回PartialView(TreeSummary);
}
}
}
GenealogyController运行正常,它是TreeController,我可以让AddToTree工作,这样我的用户就可以开始构建它了r家谱。我使用AddToTree按钮的视图是我的详细信息视图:
@model WebGenealogy。 Models.Genealogy
@ {
ViewBag.Title =详情;
布局=〜/观看/共享/ _Layout.cshtml;
}
详细信息
I am trying to build a genealogy tree for members using MVC 4, I have the membership and registration working and Blog and search working I am have difficulty building the personal tree my users. I have build two tables, Genealogy and Tree. The code for these Tables are as follows:
Genealogy Table:
CREATE TABLE [dbo].[Genealogy] (
[GenealogyId] INT IDENTITY (1, 1) NOT NULL,
[FamilyName] NVARCHAR (100) NOT NULL,
[FirstName] NVARCHAR (100) NULL,
[MiddleName1] NVARCHAR (100) NULL,
[MiddleName2] NVARCHAR (100) NULL,
[MiddleName3] NVARCHAR (100) NULL,
[Gender] NVARCHAR (10) NULL,
[DOB] DATETIME NULL,
[COOB] NVARCHAR (100) NULL,
[SOB] NVARCHAR (100) NULL,
[COB] NVARCHAR (100) NULL,
[Newsletter] NVARCHAR (10) NULL,
CONSTRAINT [PK_Genealogy] PRIMARY KEY CLUSTERED ([GenealogyId] ASC)
);
Tree Table:
CREATE TABLE [dbo].[Tree] (
[TreeId] INT IDENTITY (1, 1) NOT NULL,
[GenealogyId] INT NULL,
[RecordId] INT NULL,
[UserId] NVARCHAR (128) NULL,
[UserName] NVARCHAR (MAX) NULL,
[FamilyName] NVARCHAR (100) NULL,
[FirstName] NVARCHAR (100) NULL,
[MiddleName1] NVARCHAR (100) NULL,
[DOB] DATETIME NULL,
[Count] INT NULL,
[DateCreated] DATETIME NULL,
CONSTRAINT [PK_Tree] PRIMARY KEY CLUSTERED ([TreeId] ASC),
CONSTRAINT [FK_Tree_ToGenealogy] FOREIGN KEY ([GenealogyId]) REFERENCES [dbo].[Genealogy] ([GenealogyId]),
CONSTRAINT [FK_Tree_ToAspNetUsers] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id])
);
The Tree Table has a relationship with the AspNetUsers Table and Genealogy Table. I have also Controllers, GenealogyController and TreeBuilderController.
GenealogyController:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebGenealogy.Models;
namespace WebGenealogy.Controllers
{
public class GenealogyController : Controller
{
private GenealogyContext db = new GenealogyContext();
// GET: /Genealogy/
public ActionResult Index()
{
return View(db.Genealogy.ToList());
}
// GET: /Genealogy/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Genealogy genealogy = db.Genealogy.Find(id);
if (genealogy == null)
{
return HttpNotFound();
}
return View(genealogy);
}
// GET: /Genealogy/Create
public ActionResult Create()
{
return View();
}
// POST: /Genealogy/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="GenealogyId,FamilyName,FirstName,MiddleName1,MiddleName2,MiddleName3,Gender,DOB,COOB,SOB,COB,Newsletter")] Genealogy genealogy)
{
if (ModelState.IsValid)
{
db.Genealogy.Add(genealogy);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(genealogy);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
TreeController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGenealogy.Models;
namespace WebGenealogy.Controllers
{
public class TreeBuilderController : Controller
{
private GenealogyContext db = new GenealogyContext();
// GET: /TreeBuilder/
public ActionResult Index()
{
var Tree = TreeBuilder.GetTree(this.HttpContext);
// Set up our ViewModel
var viewModel = new TreeBuilderViewModel
{
TreeItems = Tree.GetTreeItems(),
};
// Return the view
return View(viewModel);
}
public ActionResult AddToCart(int id)
{
// Retrieve the album from the database
var addedGenealogy = db.Genealogy
.Single(Genealogy => Genealogy.GenealogyId == id);
// Add it to the shopping cart
var Tree = TreeBuilder.GetTree(this.HttpContext);
Tree.AddToTree(addedGenealogy);
// Go back to the main store page for more shopping
return RedirectToAction("Index");
}
[ChildActionOnly]
public ActionResult TreeSummary()
{
var Tree = TreeBuilder.GetTree(this.HttpContext);
ViewData["TreeCount"] = Tree.GetCount();
return PartialView("TreeSummary");
}
}
}
The GenealogyController is working fine, it is the TreeController, I can get the AddToTree to work so my user can start building their Family Trees. The View that I have with the AddToTree button is my Detail View:
@model WebGenealogy.Models.Genealogy
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Details
家谱
Genealogy
@ Html.ActionLink(添加到树,AddToTree,
TreeBuilder,新{id = Model.GenealogyId},)< br $>
@ Html.ActionLink(返回列表,索引)
@Html.ActionLink("Add to Tree", "AddToTree",
"TreeBuilder", new { id = Model.GenealogyId }, "")
@Html.ActionLink("Back to List", "Index")
推荐答案
我不确定是什么问题。我没有看到你提供的表结构,映射到树的任何方式,但如果从表中获取树是你的问题,你可以使用 CTE [ ^ ]。如果这不是你的问题,那么请澄清。
约束[FK_Tree_ToGenealogy]外键([GenealogyId])参考文献[DBO]。[家谱]([GenealogyId]) ,
让我觉得这确实是一个关于递归以使你的树脱离SQL的问题。
I am not sure what the issue is. I don't see any way that the table structure you provided, maps to a tree, but if getting a tree out of a table is your issue, you can do a recursive query with a CTE[^]. If that's not your issue, then please clarify.
CONSTRAINT [FK_Tree_ToGenealogy] FOREIGN KEY ([GenealogyId]) REFERENCES [dbo].[Genealogy] ([GenealogyId]),
is the bit that has me thinking that this is indeed a question about recursion to get your tree out of the SQL.
这篇关于设置树类型以使用用户ID并构建其个人族谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!