在哪里实例化新列表<t>?

时间:2016-04-30 13:21:06

标签: asp.net-mvc asp.net-mvc-5

我的Asp.Net Mvc项目中有以下模型。 创建新对象时,还会创建一个包含“选项”和“照片”的新列表,以便您只需向对象添加新的“选项和图片”。

public class VehicleModels
{
    [Key]
    public virtual int Id { get; set; }
    public virtual string Naam { get; set; }
    public virtual string Merk { get; set; }
    public virtual string Brandstof { get; set; }
    public virtual string Kleur { get; set; }
    public virtual string TypeVanMerk { get; set; }
    public virtual string TypeVanTransmissie { get; set; }
    public virtual int Kilometerstand { get; set; }
    public virtual int Bouwjaar { get; set; }
    public virtual int AantalDeuren { get; set; }
    public List<Optie> Options { get; set; }
    public List<Foto> Fotos { get; set; }

    public VehicleModels()
    {
        Options = new List<Optie>();
        Fotos = new List<Foto>();
    }  

}

我在控制器中有这个编辑

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int? id, HttpPostedFileBase upload)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var VehicleToUpdate = db.VehicleModels.Find(id);
            if (TryUpdateModel(VehicleToUpdate))
            {
                try
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        if (VehicleToUpdate.Fotos.Any(f => f.VehicleModelsID == VehicleToUpdate.Id))
                        {
                            db.Fotos.Remove(VehicleToUpdate.Fotos.First(f => f.VehicleModelsID == VehicleToUpdate.Id));
                        }

                        var picture = new Foto
                        {
                            FotoNaam = System.IO.Path.GetFileName(upload.FileName),
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            picture.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        VehicleToUpdate.Fotos.Add(picture);
                    }
                    db.Entry(VehicleToUpdate).State = EntityState.Modified;
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
                catch (RetryLimitExceededException /* dex */)
                {
                    //Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            return View(VehicleToUpdate);

        }

我的问题:

调试时我发现我的Edit方法中的这行代码(var VehicleToUpdate = db.VehicleModels.Find(id);)创建了一个新的模型对象,似乎将属性绑定到该新对象。 但是因为它通过了构造函数,所以两个列表都被擦掉了。

当我尝试更改对象的图片时问题就出现了,因为列表是空的,它不会删除任何内容而且它也不会添加新图像,而只是再次添加旧图像,留下我与2x相同的图像。

我应该在其他地方实例化我的列表吗? 如果是的话,哪里适当的地方?

旁注:我按照this教程将图像添加到项目中。

1 个答案:

答案 0 :(得分:0)

<ads> <ad id="bcc0d6dd-75c6-4e18-8318-a9defc8c0216" date="4/16/2016 4:00:16 AM"> <adinfo title="Wanted: iPod 5" email="message4fahad@yahoo.com" phone="7803051822" rod="" rprice="Please contact" rat="Wanted" cdetails="" address="5204 52 Ave, Rocky Mountain House, AB T4T 1G8" postal="T4T 1G8" description="Wanted: iPod 5" /> <adimg> <img main="T" src="fede4afa-9f62-4cb5-a215-9e8714247416.jpg" /> <img main="F" src="" /> </adimg> </ad> <ad id="8482fcb2-8fb0-4e7e-9b51-4ed77ac3cd5f" date="4/6/2016 1:12:44 AM"> <adinfo title="Sharp 50&quot; TV, LC-50LB261U, little over a year old" email="message4fahad@yahoo.com" phone="7802391211" rod="" rprice="475" rat="Offering" cdetails="" address=" Edmonton, AB T5T6J9" postal="T5T6J9" description="Sharp 50&quot; TV, LC-50LB261U, little over a year old" /> <adimg> <img main="T" src="2108fcd5-1074-4bbe-9d66-a69fe6b4216f.jpg" /> <img main="F" src="" /> </adimg> </ad> <ad id="4b6d750d-2bd9-4382-8a45-c580f94c23ad" date="3/30/2016 1:31:26 AM"> <adinfo title="BEST GIFT!! FREE TV AND MOVIES FOREVER!! - FREE KEYBOARD!!" email="message4fahad@yahoo.com" phone="780-293-8574" rod="" rprice="149" rat="Offering" cdetails="" address="4817 118 Ave NW, Edmonton, AB T5W 1B5" postal="T5W 1B5" description="BEST GIFT!! FREE TV AND MOVIES FOREVER!! - FREE KEYBOARD!!" /> <adimg> <img main="T" src="0c79e71f-382c-4e69-83b5-c6ce27044fb7.jpg" /> <img main="F" src="" /> </adimg> </ad> <ad id="aad706c0-1722-457e-bbb4-58b5abe1145e" date="3/18/2016 12:59:50 AM"> <adinfo title="Android TV Boxes Are Better! Credit Cards Accepted." email="message4fahad@yahoo.com" phone="780-887-8978" rod="" rprice="119.99" rat="Offering" cdetails="" address=" Strathcona County, AB T8H2G8" postal="T8H2G8" description="Android TV Boxes Are Better! Credit Cards Accepted." /> <adimg> <img main="T" src="de5cc90a-6e47-4bed-81a1-eec8945f4495.jpg" /> <img main="F" src="" /> </adimg> </ad> 不应该清除这两个列表。它们应该适当填充。

如果他们没有填充并且变为空,那么请检查您的数据库。要么数据不存在。如果数据存在,则外键约束被破坏。检查并更正相同的内容。更正数据库后,请在项目中的Entity Framework的edmx文件中更新模型。之后,实体框架应适当填充数据,图像将更新,而不是添加副本。