MVC从选定值创建脚手架

时间:2018-03-06 06:48:35

标签: c# asp.net-mvc

我有以下型号:

[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 = 8AssetID

我在AssetRegisterCreate

上创建了一个脚手架视图

我的问题是AssetID应该是ParentID的值。我怎样才能做到这一点?由于所选AssetID使用@Html.EditorFor(model => model.AssetNumber)?我是否需要创建新的@Html.EditorFor?但是在哪个领域?

请指教。 谢谢。

0 个答案:

没有答案