传递dropdownlist的选定值作为Model asp.net mvc的属性

本文关键字:asp Model net mvc 属性 dropdownlist 传递 | 更新日期: 2023-09-27 18:19:52

我正在尝试使用dropdownlist来获取我的模型的三个属性的选定值,该下拉列表使用脚本填充下一个下拉列表。

所以我的问题是,我用以下代码替换EF代码:

@Html.DropDownList("AssetID", ViewBag.AssetID as SelectList, "Select a Asset Class", new { id="AssetID" })<br />
            <select id="Segment" name="segment"></select><br />
            <select id="subAsset" name="SubAsset"></select><br />

而不是EF给出的代码:

  @Html.DropDownList("AssetID", String.Empty)
  @Html.DropDownList("SegmentID", String.Empty)
  @Html.DropDownList("SubAssetID", String.Empty)

这是我的模型限制的三个属性(外键)。我的问题是,现在modelState无效,因此Restriction的引用没有添加到数据库中,也许我不得不DropDownlistFor将所选值绑定到属性,但我不知道如何绑定。此外,如果需要,我可以发布脚本。

我的型号限制:

    public string portefeuille
    public int AssetID
    public int SegmentID
    public int SubAssetID
    public int Min
    public int Max

我的脚本用于根据以前选择的项目填充下拉列表:

  $(function () {
$('#AssetID').change(function () {
    $.getJSON('/Restriction/SegmentList/' + $('#AssetID').val(), function (data) {
        var items = '<option>Select a Segment</option>';
        $.each(data, function (i, segment) {
            items += "<option value='" + segment.Value + "'>" + segment.Text + "</option>";
        });
        $('#Segment').html(items);
    });
});
$('#Segment').change(function () {
    $.getJSON('/Restriction/SubAssetList/' + $('#Segment').val(), function (data) {
        var items = '<option>Select a SubAsset</option>';
        $.each(data, function (i, subAsset) {
            items += "<option value='" + subAsset.Value + "'>" + subAsset.Text + "</option>";
        });
        $('#subAsset').html(items);
    });
});

});

这是我的限制控制器:

  public class RestrictionController : Controller
{
    private RestrictionContext db = new RestrictionContext();
    //
    // GET: /Restriction/
    public ActionResult Index()
    {
        var restrictions = db.Restrictions.Include(r => r.Asset).Include(r => r.Segment).Include(r => r.SubAsset);
        return View(restrictions.ToList());
    }
    //
    // GET: /Restriction/Details/5
    public ActionResult Details(int id = 0)
    {
        Restriction restriction = db.Restrictions.Find(id);
        if (restriction == null)
        {
            return HttpNotFound();
        }
        return View(restriction);
    }
    //
    // GET: /Restriction/Create
    public ActionResult Create()
    {
        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name");
        /*
        ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name");
        ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_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);
        /*
        ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name", restriction.SegmentID);
        ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_Name", restriction.SubAssetID);
         */
        return View(restriction);
    }
    //
    // GET: /Restriction/Edit/5
    public ActionResult Edit(int id = 0)
    {
        Restriction restriction = db.Restrictions.Find(id);
        if (restriction == null)
        {
            return HttpNotFound();
        }
        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID);
        ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name", restriction.SegmentID);
        ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_Name", restriction.SubAssetID);
        return View(restriction);
    }
    //
    // POST: /Restriction/Edit/5
    [HttpPost]
    public ActionResult Edit(Restriction restriction)
    {
        if (ModelState.IsValid)
        {
            db.Entry(restriction).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID);
        ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name", restriction.SegmentID);
        ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_Name", restriction.SubAssetID);
        return View(restriction);
    }
    //
    // GET: /Restriction/Delete/5
    public ActionResult Delete(int id = 0)
    {
        Restriction restriction = db.Restrictions.Find(id);
        if (restriction == null)
        {
            return HttpNotFound();
        }
        return View(restriction);
    }
    //
    // POST: /Restriction/Delete/5
    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        Restriction restriction = db.Restrictions.Find(id);
        db.Restrictions.Remove(restriction);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
    public JsonResult SegmentList(int Id)
    {
        var segment = from s in db.Segments
                      where s.AssetID == Id
                      select s;
        return Json(new SelectList(segment.ToArray(), "SegmentID", "Segment_Name"), JsonRequestBehavior.AllowGet);
    }
    public JsonResult SubAssetList(int id)
    {
        var subAsset = from c in db.SubAssets
                       where c.SegmentID == id
                       select c;
        return Json(new SelectList(subAsset.ToArray(), "SubAssetID", "SubAsset_Name"), JsonRequestBehavior.AllowGet);
    }
    public IList<Segment> Getsegment(int AssetID)
    {
        return db.Segments.Where(m => m.AssetID == AssetID).ToList();
    }
    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult LoadClassesByAssetId(string Asset_Name)
    {
        var segmentList = this.Getsegment(Convert.ToInt32(Asset_Name));
        var segmentData = segmentList.Select(m => new SelectListItem()
        {
            Text = m.Segment_Name,
            Value = m.AssetID.ToString(),
        });
        return Json(segmentData, JsonRequestBehavior.AllowGet);
    }
}

谢谢你的帮助!

传递dropdownlist的选定值作为Model asp.net mvc的属性

首先,您的模型属性上没有getter和setter。

其次,您的模型具有属性SegmentIDsubAssetID,但您已将控件命名为SegmentsubAsset,因此它们与您的属性名称不匹配。您需要将它们命名为SegmentIDSubAssetID。我强烈建议使用强类型的html辅助方法来防止这种情况,例如@Html.DropDownListFor(m => m.AssetID, ViewBag.AssetID as SelectList, ""Select a Asset Class", null)