I have two models with a navigation property in one to the other. Model looks like this: I am calling the two models in the same view. SuiteCategoryModels as the default and SuitesModels as a partial view.
public class SuiteCategoryModels
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int SuiteCatID { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Category")]
[StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
public string CatName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Category Description")]
[StringLength(500, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
public string CatDesc { get; set; }
public virtual ICollection<SuitesModels> SuitesModels { get; set; }
}
and the second model like this:
public class SuitesModels
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int SuiteID { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Suite Name")]
[StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
public string SuiteName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Description")]
[StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
public string SuiteDesc { get; set; }
[DataType(DataType.ImageUrl)]
[Display(Name = "Picture")]
public string SuitePix { get; set; }
[Required]
[Display(Name = "Rate")]
public int SuiteRate { get; set; }
[Required]
[Display(Name = "Persons Per Suite")]
public int PersonPerSuite { get; set; }
[Required]
[Display(Name = "Status")]
public bool SuiteStatus { get; set; }
public int SuiteCatID { get; set; }
public virtual SuiteCategoryModels SuiteCategory { get; set; }
}
I created a controller action to view like this:
public ActionResult Index()
{
var m = db.SuiteCategoryModels.ToList();
//ViewBag.BB =
//Dispose(false);
return View(m);
}
and the other like this:
public ActionResult SuitesIndex(int id = 0)
{
var mm = db.Suites.Select(r => r.SuiteCategory)
.Where(r => r.SuiteCatID == id);
//SuitesModels suitesmodels = db.Suites.Find(id);
if (mm == null)
{
return HttpNotFound();
}
return PartialView("SuitesIndex", mm.ToList());
}
But I have an error page stating: There is already an open DataReader associated with this Command which must be closed first.
.
- What are the reasons of this error?
- What do I do to correct this?