There are two tables: a news table and a table of pictures. In the table of pictures there is a NewsId field which is associated with a news Id .

Here is the model:

 public class RelNewsDto { public int Id { get; set; } public string Title { get; set; } public string Body { get; set; } public virtual ICollection<NewsImageDto> NewsImages { get; set; } } public class NewsImageDto { public int Id { get; set; } public byte[] ImageItem { get; set; } public int NewsId { get; set; } } 

I can't figure out how to add images to the NewsImages collection NewsImages

Here is what happens in View:

 @using (Html.BeginForm("RelNewsCreate", "News", FormMethod.Post, new { enctype = "multipart/form-data" })) { <h4>Title</h4> @Html.TextBoxFor(model => model.Title, new { @class = "form-control", required = "required", autocomplete = "off" }) <br /> <h4>Images</h4> @Html.TextBoxFor(Model => Model.NewsImages, new { type = "file", name = "uploadImage", min = "1", max = "9999", multiple = "true" }) <br /> <h4>Article</h4> @Html.TextAreaFor(model => model.Body, 20, 0, new { @class = "form-control", required = "required", autocomplete = "off" }) <br /> <input type="submit" value="Save" class="btn btn-success" id="btn-save" /> } 

This is what happens in the controller:

  [HttpPost] public ActionResult RelNewsCreate(BLL.Model.RelNewsViewModelItem item) { BLL.Providers.RelNewsBdProvider dbProv = new BLL.Providers.RelNewsBdProvider(); for (var i = 0; i < Request.Files.Count; i++) { var image = Request.Files[i]; if (image != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(image.InputStream)) { imageData = binaryReader.ReadBytes(image.ContentLength); item.NewsImages.Add(new BLL.Model.NewsImageViewModelItem { NewsId = item.Id, ImageItem = imageData }); } } } dbProv.Add(item); return RedirectToAction("RelNews"); } 

Business Logic Layer:

  public void Add(RelNewsViewModelItem newsItem) { dbProv.Add(MapDtoToDb(newsItem)); } public RelNewsDto MapDtoToDb(RelNewsViewModelItem dbItem) { if (dbItem != null) { return new RelNewsDto { Id = dbItem.Id, Title = dbItem.Title, Body = dbItem.Body, NewsImages = dbItem.NewsImages.Select(MapNewsImageDto).ToList() }; } return null; } 

Data Access Layer:

  public void Add(RelNewsDto newsItem) { using (var db = new DataBaseDataContext()) { db.NewsVs.InsertOnSubmit(MapDtoToDb(newsItem)); var images = newsItem.NewsImages.Select(x => new NewsImage() { Id = x.Id, ImageItem = x.ImageItem, NewsId = newsItem.Id }).ToList(); db.NewsImages.InsertAllOnSubmit(images); db.SubmitChanges(); } } public NewsV MapDtoToDb (RelNewsDto dbItem) { if (dbItem != null) { return new NewsV { Id = dbItem.Id, Title = dbItem.Title, Body = dbItem.Body }; } return null; } 

Reaches the last line ( db.SubmitChanges(); ) and gives an exception:

System.Data.SqlClient.SqlException l System.Data.Linq.dll but it is not handled in user code

Additional information: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_NewsImage_NewsV". Occurred in database "Schedule", table "dbo.NewsV", column 'Id'.

I do not understand how to correct the error.

  • About architecture. Do you want to make a structure in the database for storing an array of bytes or do you want to store in the database only the file name, and the file on the disk? About the task itself. I do not understand what you have plugging in when performing a task. In order to save the data that came from the POST, in order to read the data from the database to your Dto'shki or what? - AK
  • one
    What for? POST came - saved the pictures to the database, made a redirect to some other page. If you need to show - counted from the base. - AK
  • It seems in my head two questions mixed up. )) I just answered a related topic and thought it was the same author. - AK
  • @AK I have a website if there are Data Access Layer and Business Logic Layer layers. I get the pictures to the controller and send them to Business Logic using a loop, but I cannot figure out how to convert pictures from Business Logic and send them to Data Access. - Nikita
  • one
    Ok, can not figure it out. Show then (write code in question) what do you have in BL and DAL from code. You apparently have a repository there with CRUD, it’s not clear without seeing the code to understand where you are plugged. Write the code, do not be afraid! I get the pictures in the controller - attributed the code, send them to the BL - attributed the code, so until you reach the place of the plug. The code in question will help you understand better than your explanations "there I have such and such code, and there such and such." Immediately write the code will be clearer. We don't have a telepathy competition here, right? - AK

1 answer 1

There is no more error.

 public void Add(RelNewsDto newsItem) { using (var db = new DataBaseDataContext()) { var item = MapDtoToDb(newsItem); var images = newsItem.NewsImages.Select(MapNewsImage).ToList(); db.NewsVs.InsertOnSubmit(item); item.NewsImages.AddRange(images); db.SubmitChanges(); } }