密钥未在IFormCollection中显示

时间:2018-01-06 09:23:33

标签: c# asp.net-mvc asp.net-core

我试图从一个在asp.net核心应用程序中发布的表单中读取值。显示的poco具有类型List<PictureModel>的图片属性。所以我想,我可以通过传递给控制器​​中的ActionsResult的IFormCollection来访问集合的每个图片。但是,如果查看集合中的键,我只会看到Id和Title。例如,缺少条目PictureModel_6。为什么这个密钥丢失了?

查看              

            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="col-md-4 col-lg-4">
                <div class="form-group">
                    <label asp-for="Id" class="control-label"></label>
                    <input asp-for="Id" class="form-control" />
                    <span asp-validation-for="Id" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Title" class="control-label"></label>
                    <input asp-for="Title" class="form-control" />
                    <span asp-validation-for="Title" class="text-danger"></span>
                </div>
            </div>

            <div class="col-md-4 col-lg-4">

                <div>
                    <!-- As soon as one picture is selected in the fileopen dialog post the form back and reload the page with the added picture -->
                    <label class="btn btn-default btn-file">
                        Add Picture <input type="file" onchange="this.form.submit()" style="display: none;" id="uploadFile" name="uploadFile" value="uploadFile">
                    </label>

                    <label class="control-label col-md-4 col-lg-4" for="PicTitle">Pictures</label>
                </div>

                <div class="BilderVertikal">
                    @foreach (Models.PictureModel b in Model.Pictures)
                            {

                                var base64 = Convert.ToBase64String(b.DbBild);
                                var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
                                string formName = string.Format("{0}_{1}", b.GetType().Name, b.Id);


                        <img name="@formName" src="@imgSrc" />
                    }
                </div>

            </div>

        <div class="form-group">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>

    </div>
</form>

模型

public class MasterModel 
{
    public MasterModel()
    {

    }

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value;  }
    }

    private string title;

    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    private List<PictureModel > pictures;

    public List<PictureModel > Pictures
    {
        get { return pictures; }
        set { pictures = value;  }
    }
}

public class PictureModel 

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value;  }
    }

    private byte[] dbPicture;

    public byte[] DbPicture
    {
        get { return dbPicture; }
        set { dbPicture = value; }
    }
}

控制器

[HttpPost]
public ActionResult Save(string id, IFormCollection collection, IFormFile uploadFile)
{
    MasterModel m = new MasterModel();
    m.Title = collection["Title"].ToString(); //collection contains the key "Title"


    if (uploadFile != null)     
    {//Add Picture to the model without saving it

        PictureModel b = new PictureModel();
        MemoryStream target = new MemoryStream();
        uploadFile.CopyTo(target);                               
        b.DbBild = target.ToArray();

        m.Pictures.Add(b);

        return RedirectToAction(string.Format("Edit/{0}", m.Id));
    }

    foreach (var key in collection) 
    {
        if (key.ToString().StartsWith("PictureModel"))
        {
            //nothing found here
        }
    }       
}

解决方案

感谢@Stephen Muecke的大力帮助,我将代码改为此。这将为IFormCollection中的每个图片创建一个密钥,这不再是必需的,因为集合也很好地绑定到模型。

查看

 <div >
    @for (var i = 0; i < Model.Pictures.Count; i++)

    {

        var base64 = Convert.ToBase64String(Model.Pictures[i].DbPicture);
        var imgSrc = String.Format("data:image/gif;base64,{0}", base64);


        @Html.HiddenFor(x => x.Pictures[i].Id)
        @Html.HiddenFor(x => x.Pictures[i].DbPicture)
        <img asp-for="Pictures.DbPicture" src="@imgSrc"/>
    }
</div>

控制器:

[HttpPost]
public ActionResult Save(MasterModel _m)

参数_m现在包含填充的Pictures属性,因此不需要IFormCollection

0 个答案:

没有答案