Good afternoon, I am new to ASP.NET and I cannot understand why my code is not working. I have a model:
using System.ComponentModel.DataAnnotations; using System.Drawing; using System.Globalization; namespace WebApp.Models { public class News { public int NewsId { get; set; } public string title { get; set; } public string description { get; set; } public virtual Picture picture { get; set; } public int guid { get; set; } } public class Picture { public int PictureId { get; set; } public byte[] image { get; set; } public int width { get; set; } public int height { get; set; } public int hash { get; set; } } }
And I'm trying to create new "News" through the message form:
// POST: News/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(News news, HttpPostedFileBase uploadImage) { if (ModelState.IsValid && uploadImage != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(uploadImage.InputStream)) { imageData = binaryReader.ReadBytes(uploadImage.ContentLength); } news.picture = new Picture() { hash = 0, image = imageData, width = 0, height = 0 }; db.News.Add(news); db.SaveChanges(); return RedirectToAction("Index"); } return View(news); }
But when I take data from db, I get an exception from the null pointer: when I call the debugger, I find out that the value of "news.picture" is null. But before db.SaveChanges () it is 100% non-zero. Looks like I'm doing something stupid, bcs. I can not find someone who ran into this problem. thanks.
source share