美好的一天,我是ASP.NET的新手,我无法弄清楚为什么我的代码不起作用。我有模特:
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; }
}
}
我正在努力创造新的"新闻"通过邮寄表格:
// 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);
}
但是当我从db检索数据时,我得到空指针异常: 当我打电话给调试器时,我发现了" news.picture" value为null。但在db.SaveChanges()之前它100%不为null。看起来我正在做一些愚蠢的错误,但是我找不到那些遇到这个问题的人。 THX。
答案 0 :(得分:0)
尝试以下
[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);
}
var picture = new Picture()
{
hash = 0,
image = imageData,
width = 0,
height = 0
};
db.Pictures.Add(picture); // make sure that the picture was tracked by the EF
news.picture = picture;
db.News.Add(news);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
答案 1 :(得分:0)
我会做出以下更改,看看是否有帮助。
namespace WebApp.Models
{
public class News
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[ForeignKey("Picture")]
public int PictureId { get; set; }
public virtual Picture Picture { get; set; }
// not sure why this is here
public int guid { get; set; }
}
public class Picture
{
public int Id { get; set; }
public byte[] ImageData { get; set; }
public string ContentType { get; set; }
public int width { get; set; }
public int height { get; set; }
public int hash { get; set; }
}
}
控制器:
// 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(NewsDto news, HttpPostedFileBase uploadImage)
{
if (ModelState.IsValid && uploadImage != null)
{
var imageData = byte[uploadImage.ContentLength];
using (var binaryReader = new BinaryReader(uploadImage.InputStream))
{
imageData = binaryReader.ReadBytes(uploadImage.ContentLength);
}
var newsEntity = new New {
Title = news.Title,
Description = news.Description,
Picture = new Picture()
{
ImageData = imageData,
ContentType = contentType // I would get that from the HttpPostedFileBase
}
}
// I always wrap my database connections in a using.
using (var db = new DbContext()) {
db.News.Add(newsEntity);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(news);
}
答案 2 :(得分:0)
我不知道为什么,但是当我添加虚拟财产时,所有工作都有效:
public virtual Picture picture { get; set; }