在不使用Html.EditorForasp.net mvc的情况下创建模型引用时出现问题

本文关键字:引用 模型 创建 问题 情况下 Html EditorForasp mvc net | 更新日期: 2023-09-27 18:19:55

我在尝试创建模型限制的引用时遇到问题:

    public int RestrictionID
    public string portefeuille
    public int AssetID
    public int SegmentID
    public int SubAssetID
    public int Min
    public int Max
    public string logic_op
    public virtual Asset Asset
    public virtual Segment Segment
    public virtual SubAsset SubAsset

这不是在@Html.EditorFor的创建视图中使用普通模板。因为我使用的是一个下拉列表,后面有一个脚本来填充下拉列表,这取决于以前选择的项目,效果很好,但提交时没有发生任何错误,没有重定向,当然引用也不会添加到数据库中。这是我的创建视图:

  <div class="editor-label">
        @Html.LabelFor(model => model.portefeuille)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.portefeuille)
        @Html.ValidationMessageFor(model => model.portefeuille)
    </div>
    @Html.DropDownList("Asset", ViewBag.AssetID as SelectList, "Select a Asset Class", new { id="Asset" })<br />
            <select id="Segment" name="segment"></select><br />   //Here I am not using Html.EditorFor as EF does
            <select id="subAsset" name="SubAsset"></select><br />
    <div class="editor-label">
        @Html.LabelFor(model => model.Min)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Min)
        @Html.ValidationMessageFor(model => model.Min)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Max)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Max)
        @Html.ValidationMessageFor(model => model.Max)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.logic_op)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.logic_op)
        @Html.ValidationMessageFor(model => model.logic_op)
    </div>

和我的RestrictionController:(我省略了无用的部分)

    public ActionResult Create()
    {
        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name");
        return View();
    }
    //
    // POST: /Restriction/Create
    [HttpPost]
    public ActionResult Create(Restriction restriction)
    {
        if (ModelState.IsValid)
        {
            db.Restrictions.Add(restriction);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID);
        return View(restriction);
    }

有人能帮忙找出问题出在哪里吗?

谢谢你的帮助!

在不使用Html.EditorForasp.net mvc的情况下创建模型引用时出现问题

模型a中属性的名称应该与html中元素的名称相同,以便在post back后绑定值。这就是为什么下面的代码

 <select id="Segment" name="segment"></select><br />   //Here I am not using Html.EditorFor as EF does
            <select id="subAsset" name="SubAsset"></select><br />

应替换为.

 <select id="Segment" name="SegmentID"></select><br />   //Here I am not using Html.EditorFor as EF does
            <select id="subAsset" name="SubAssetID"></select><br />

始终查看由任何技术生成的HTML。这将帮助你了解事情是如何运作的。