我有以下型号:
[Key]
public int AssetID { get; set; }
[Required]
public string AssetNumber { get; set; }
public string AssetDescription { get; set; }
public int? ParentAssetID { get; set; }
[ForeignKey("ParentAssetID")]
public virtual AssetRegister Parent { get; set; }
public virtual ICollection<AssetRegister> Childs { get; set; }
然后我有以下控制器:
public ActionResult AssetRegisterCreate(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
AssetRegister assetRegister = db.AssetRegisters.Find(id);
if (assetRegister == null)
{
return HttpNotFound();
}
return View(assetRegister);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AssetRegisterCreate([Bind(Include = "AssetNumber,AssetDescription,ParentAssetID")] AssetRegister assetRegister)
{
if (ModelState.IsValid)
{
db.Entry(assetRegister).State = EntityState.Added;
db.SaveChanges();
return RedirectToAction("TreeView");
}
return View(assetRegister);
}
在TreeView
视图中,我有以下命令:
@using (Html.BeginForm("AssetRegisterCreate", "AssetRegister", FormMethod.Get))
{
<input type="text" id="AssetID" name="id" />
<button type="submit">Create</button>
}
点击Create
按钮后,它会将我重定向到AssetRegister/AssetRegisterCreate?id=8
id = 8
是AssetID
。
我在AssetRegisterCreate
:
我的问题是AssetID应该是ParentID的值。我怎样才能做到这一点?由于所选AssetID使用@Html.EditorFor(model => model.AssetNumber)
?我是否需要创建新的@Html.EditorFor
?但是在哪个领域?
请指教。 谢谢。