从视图向控制器发送一个内部列表的实体对象

时间:2019-05-29 15:33:46

标签: c# asp.net-mvc entity-framework razor

我得到了这个物体:

    public ToonMoves()
    {
        this.Effects = new HashSet<Effects>();
        this.MoveInfo = new HashSet<MoveInfo>();
    }

    public long ToonMovesId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public byte[] Icon { get; set; }
    public string Target { get; set; }
    public Nullable<long> TypeId { get; set; }
    public Nullable<int> Cooldown { get; set; }
    public string LegendaryName { get; set; }
    public string LegendaryDescription { get; set; }
    public string LegendaryName2 { get; set; }
    public string LegendaryDescription2 { get; set; }
    public long ToonId { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Effects> Effects { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<MoveInfo> MoveInfo { get; set; }
    public virtual Toon Toon { get; set; }
    public virtual Type Type { get; set; }

我需要从视图发送动作,已经起作用的效果,每个动作都有X数量的效果,因此在视图上我们可以选择X数量的效果:

    <div class="form-group">
        @Html.LabelFor(model => model.Effects, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("EffectsId", (MultiSelectList)ViewBag.Effects, htmlAttributes: new { multiple = "multiple", @class = "selectpicker" })               
            @Html.ValidationMessageFor(model => model.Effects, "", new { @class = "text-danger" })
        </div>
    </div>

在控制器上,我通过多选发送列表:

    public ActionResult Create()
    {
        ViewBag.Effects = new MultiSelectList(effectsService.GetAll(), "EffectsId", "Name");
        ViewBag.ToonId = new SelectList(toonService.GetAll(), "ToonId", "Name");
        ViewBag.TypeId = new SelectList(typeService.GetAll(), "TypeId", "Name");
        return View();
    }

但是当我提交给控制器时,我的效果对象为空,列表和move.effect ...

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ToonMoves toonMoves, HttpPostedFileBase imgMove, List<Effects> effects)
    {
        toonMoves.Icon = new byte[imgMove.ContentLength];
        imgMove.InputStream.Read(toonMoves.Icon, 0, imgMove.ContentLength);

        if (ModelState.IsValid)
        {
            movesService.Save(toonMoves);
            return RedirectToAction("Index");
        }

        ViewBag.EffectsId = new SelectList(effectsService.GetAll(), "EffectsId", "Name");
        ViewBag.TypeId = new SelectList(typeService.GetAll(), "TypeId", "Name");
        return View(toonMoves);
    }

附加信息:我正在使用选择选择器,并且在选择值时可以看到一切正常,但在控制器上却不显示

0 个答案:

没有答案