我在文件上传时遇到问题,其中一个字段(DocumentDepartment)值返回 null,而所有其他字段信息都被保存。
请帮忙
我的模型有以下代码:
public class Document
{
public int DocumentID { get; set; }
public string DocumentName { get; set; }
public string DocumentDepartment { get; set; }
public byte[] DocumentType { get; set; }
}
我的控制器中的代码:
[HttpPost]
public ActionResult Create(HttpPostedFileBase document)
{
// verify user selected a file
if (document != null && document.ContentLength > 0)
{
//Get file information
var documentName = Path.GetFileName(document.FileName);
var contentLength = document.ContentLength;
var contenttype = document.ContentType;
var datadept = DocumentDepartment;
//Get the file data
byte[] data = new byte[] { };
using (var binaryReader = new BinaryReader(document.InputStream))
{
data = binaryReader.ReadBytes(document.ContentLength);
}
//Save to the database
Document doc = new Document();
doc.DocumentDepartment = datadept;
doc.DocumentName = documentName;
doc.DocumentType = data;
//show success....
db.Document.Add(doc);
db.SaveChanges();
ViewData["message"] = document.FileName + "has been saved.";
return RedirectToAction("Index");
}
return View(document);
}