Hey guys, I really need your guidance and assistance. I have a method which gets a list of items from database:
My database tables:
public class Item
{
[Key]
public int ItemId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string ItemArtUrl { get; set; }
public virtual int CategoryId { get; set; }
public virtual Category Categories { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
My viewmodels:
public class ItemViewModel
{
[Key]
public int ItemId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string ItemArtUrl { get; set; }
public int CategoryId { get; set; }
public IEnumerable<SelectListItem> MyCategories { get; set; }
}
public class CategoryBusiness : ICategoryBusiness
{
public List<CategoryViewModel> GetAllCategories()
{
var categoryrepo = new CategoryRepository();
var query = (from x in categoryrepo.GetAll()
select new CategoryViewModel()
{
CategoryId = x.CategoryId,
Name = x.Name,
Description = x.Description
}).ToList();
return query;
}
}
In my controller [HttpGet] Create method, I create a drop down list which will be rendered in my view:
[HttpGet]
public ActionResult Create()
{
var categoryBusiness = new CategoryBusiness();
ViewBag.items = new SelectList(categoryBusiness.GetAllCategories(), "CategoryId", "Name");
return View();
}
[HttpPost]
public ActionResult Create(ItemViewModel item)
{
var itemBusiness = new ItemBusiness();
itemBusiness.Insert(item);
return View();
}
My Html code is as follows:
@model DemocraticLiberalCongress.Model.ItemViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("items", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
When I debug the program it renders the dropdown. However when I commit to the database. I get the following error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Items_dbo.Categories_CategoryId". The conflict occurred in database "DLCDataContext", table "dbo.Categories", column 'CategoryId'.
The statement has been terminated.
Thanks
My database tables:
public class Item
{
[Key]
public int ItemId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string ItemArtUrl { get; set; }
public virtual int CategoryId { get; set; }
public virtual Category Categories { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
My viewmodels:
public class ItemViewModel
{
[Key]
public int ItemId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string ItemArtUrl { get; set; }
public int CategoryId { get; set; }
public IEnumerable<SelectListItem> MyCategories { get; set; }
}
public class CategoryBusiness : ICategoryBusiness
{
public List<CategoryViewModel> GetAllCategories()
{
var categoryrepo = new CategoryRepository();
var query = (from x in categoryrepo.GetAll()
select new CategoryViewModel()
{
CategoryId = x.CategoryId,
Name = x.Name,
Description = x.Description
}).ToList();
return query;
}
}
In my controller [HttpGet] Create method, I create a drop down list which will be rendered in my view:
[HttpGet]
public ActionResult Create()
{
var categoryBusiness = new CategoryBusiness();
ViewBag.items = new SelectList(categoryBusiness.GetAllCategories(), "CategoryId", "Name");
return View();
}
[HttpPost]
public ActionResult Create(ItemViewModel item)
{
var itemBusiness = new ItemBusiness();
itemBusiness.Insert(item);
return View();
}
My Html code is as follows:
@model DemocraticLiberalCongress.Model.ItemViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("items", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
When I debug the program it renders the dropdown. However when I commit to the database. I get the following error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Items_dbo.Categories_CategoryId". The conflict occurred in database "DLCDataContext", table "dbo.Categories", column 'CategoryId'.
The statement has been terminated.
Thanks