Witam,
Na wstępie powiem, że jestem początkujący wiec proszę o litość
Piszę aplikację webową w której są dwie klasy w modelu
Publication:
amespace ManagerPublikacji.Domain.Entities
{
public class Publication
{
[HiddenInput(DisplayValue=false)]
public int PublicationID { get; set; }
[Required(ErrorMessage="Proszę podać nazwę publikacji.")]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Proszę podać opis publikacji.")]
public string Description { get; set; }
[HiddenInput(DisplayValue = false)]
[ForeignKey("Category")]
public int CategoryId { get; set; }
public byte[] PdfData { get; set; }
[HiddenInput(DisplayValue = false)]
public string PdfMimeType { get; set; }
[HiddenInput(DisplayValue = false)]
public int UsersID { get; set; }
public virtual Category Category { get; set; }
}
}
Category:
namespace ManagerPublikacji.Domain.Entities
{
public class Category
{
[HiddenInput(DisplayValue = false)]
public int CategoryId { get; set; }
[Required(ErrorMessage = "Proszę podać nazwę Kategorii.")]
public string CategoryName { get; set; }
public virtual ICollection<Publication> Publications { get; set; }
}
}
Podczas próby dodania do bazy publikacji

Występuje błąd:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Publication_dbo.Category_CategoryId". The conflict occurred in database "Publikacje", table "dbo.Category", column 'CategoryId'.
The statement has been terminated.

Kod kontrolera odpowiedzialny za te akcje:
public ActionResult Edit(int publicationId)
{
Publication publication = repositoryP.Publications
.FirstOrDefault(p => p.PublicationID == publicationId);
PopulateCategoriesDropDownList(publication.CategoryId);
return View(publication);
}
[HttpPost]
public ActionResult Edit(HttpPostedFileBase pdf, [Bind(Include = "PublicationId,Name,Description,CategoryId,PdfData,PdfMimeType,UsersID")] Publication publication)
{
if (ModelState.IsValid)
{
if (pdf != null)
{
publication.PdfMimeType = pdf.ContentType;
publication.PdfData = new byte[pdf.ContentLength];
pdf.InputStream.Read(publication.PdfData, 0, pdf.ContentLength);
}
publication.UsersID = (int)WebSecurity.CurrentUserId;
repositoryP.SavePublication(publication);
TempData["message"] = string.Format("Zapisano {0}", publication.Name);
return RedirectToAction("Index");
}
else
{
PopulateCategoriesDropDownList(publication.CategoryId);
return View(publication);
}
}
private void PopulateCategoriesDropDownList(object selectedCategory = null)
{
var categoriesQuery = from d in repositoryC.Categories
orderby d.CategoryName
select d;
ViewData["catid"] = new SelectList(categoriesQuery, "CategoryId ", "CategoryName", selectedCategory);
}
public ViewResult Create()
{
PopulateCategoriesDropDownList();
return View("Edit", new Publication());
}
oraz widoku:
@model ManagerPublikacji.Domain.Entities.Publication
@{
ViewBag.Title = "Admin: edycja " + @Model.Name;
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h1>Edycja @Model.Name</h1>
@using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.EditorForModel()
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, "CategoryId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("catid")
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
<div class="editor-label">Pdf</div>
<div>Prześlij pdf: <input type="file" name="PDF"/></div>
<input type="submit" value="Zapisz"/>
@Html.ActionLink("Anuluj i wróć do listy ", "Index")
}
Z tego co zauważyłem to drop down list albo przekazuje do publication.CategoryId wartość 0 albo w ogole nie przekazuje i jest ona ustawiana domyslnie. Swój kod opierałem w dużej mierze na książce "ASP.NET MVC 4 Zaawansowane Programowanie" Adama Freemana. Proszę o pomoc i z góry za takową dziękuję.