0

I developed a gallery management system, nothing fancy, it has two models:

public class Category
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "שם קטגוריה")]
    [Required(ErrorMessage="אנא הכנס שם קטגוריה", AllowEmptyStrings=false)]
    public string Name { get; set; }

    [Display(Name = "הצג")]
    [Required(ErrorMessage="אנא בחר האם להציג קטגוריה)")]
    public bool Display { get; set; }

    public List<Image> Images { get; set; }
}

public class Image
{
    [Key]
    public int Id { get; set; }

    [Display(Name="שם תמונה")]
    [Required(ErrorMessage="אנא בחר שם תמונה")]
    [MaxLength(50)]
    public string Name { get; set; }

    [Display(Name="נתיב קובץ")]
    [Required(ErrorMessage="אנא בחר נתיב לתמונה")]
    public string FilePath { get; set; }

    [Display(Name = "תיאור תמונה")]
    [MaxLength(150)]
    public string Description { get; set; }

    [Display(Name = "הצג")]
    [Required]
    public bool Display { get; set; }

    [Display(Name = "קטגוריות")]
    //[Required(ErrorMessage="אנא בחר קטגוריה")]
    public List<Category> Categories { get; set; }

    [Display(Name = "נתיב דף פייסבוק")]
    public string FacebookPage { get; set; }

}

Now for the Controller:

    private CakesContext db = new CakesContext();

    public ActionResult EditImage(int id = 0)
    {
        Image image = db.Images.Find(id);
        if (image == null)
        {
            return HttpNotFound();
        }

        ViewBag.Categories = db.Categories.ToList();

        return View(image);
    }

    //
    // POST: /Categories/Edit/5

    [HttpPost]
    public ActionResult EditImage(Image image, FormCollection fc)
    {
        var categoriesString = string.IsNullOrEmpty( fc["categories"].ToString()) ? "" : fc["categories"].ToString();

        var chosenCategories = splitCategoriesString(categoriesString);
        if (chosenCategories == null || chosenCategories.Count == 0)
        {
            ModelState.AddModelError("עליך לבחור לפחות קטגוריה אחת", new Exception());
        }

        ModelState.Remove("Categories");

        if (ModelState.IsValid)
        {
            image.Categories = chosenCategories;
            db.Entry(image).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("ImageList");
        }
        return View(image);
    }

Right before I SaveChanges I debug and all the data contained in the image including the picked categories (I excluded the views for that reason, it working up to this point).

Now the weird thing, when I check the tables the data is there, I got a CategoryImages table containing only the two foreign keys: categoryId and imageId, but when I try to retrieve the data I get the image object with the Categories property set to null, any idea why?

The code for pulling the data from the DB is simple as:

var images= db.Images.ToList();
4

1 回答 1

2

导航属性需要是virtualand ICollection,试试

public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Category> Categories { get; set; }

请参阅关系和导航属性

于 2013-10-23T19:53:18.037 回答