我正在創建MVC應用程序,我使用實體框架。最後想創建文件上傳和查看。我有控制器項目控制器文件上載文件上傳和視圖不能在MVC中工作
public async Task<ActionResult> FileManager(int ProjectId = 0)
{
ReadCookieValue cookie = new ReadCookieValue();
clientId = cookie.readuserinfo().ClientId;
userId = cookie.readuserinfo().Id;
roleId = cookie.readuserinfo().RoleId;
var model = await _project.FilesList(ProjectId, clientId);
return View(model);
}
public async Task<ActionResult> FileUpload(int ProjectId = 0)
{
return View(ProjectId);
}
[HttpPost]
public async Task<ActionResult> FileUploadHandler(int ProjectId = 0)
{
ReadCookieValue cookie = new ReadCookieValue();
clientId = cookie.readuserinfo().ClientId;
userId = cookie.readuserinfo().Id;
roleId = cookie.readuserinfo().RoleId;
if (ProjectId != 0)
{
foreach (var fileKey in Request.Files.AllKeys)
{
ProjectFileModel model = null;
var file = Request.Files[fileKey];
try
{
if (file != null)
{
model = new ProjectFileModel();
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), clientId.ToString(), ProjectId.ToString());
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var path1 = Path.Combine(path, fileName);
model.ProjectId = ProjectId;
model.FileName = fileName;
model.FilePath = path;
model.UploadedBy = User.Identity.Name;
model.UploadedDate = DateTime.UtcNow;
model.ClientId = clientId;
var result = await _project.UploadFiles(model);
file.SaveAs(path1);
}
}
catch (Exception ex)
{
return Json(new { Message = "Error in saving file" });
}
}
}
else
{
return Json(new { Message = "Please Select Project" });
}
return Json(new { Message = "File saved" });
}
後,我創建的項目,我必須查看按鈕打開上傳file.click視圖按鈕我得到錯誤
{"Object reference not set to an instance of an object."}
Inner Exception null
其實我使用實體框架模型和我使用自己的模型。
文件上傳代碼
<div class="ibox-content">
<ng-form id="my-awesome-dropzone" class="dropzone" action="@Url.Action("FileUploadHandler", "Projects")" method="post" enctype="multipart/form-data">
<div class="dropzone-previews"></div>
<button type="submit" class="btn btn-primary pull-right">Submit this form!</button>
</ng-form>
</div>
這裏使用FileUploadHandler控制器Action.Here FileuploadHandler代碼
public async Task<ActionResult> FileUploadHandler(int ProjectId = 0)
{
ReadCookieValue cookie = new ReadCookieValue();
clientId = cookie.readuserinfo().ClientId;
userId = cookie.readuserinfo().Id;
roleId = cookie.readuserinfo().RoleId;
if (ProjectId != 0)
{
foreach (var fileKey in Request.Files.AllKeys)
{
ProjectFileModel model = null;
var file = Request.Files[fileKey];
try
{
if (file != null)
{
model = new ProjectFileModel();
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), clientId.ToString(), ProjectId.ToString());
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var path1 = Path.Combine(path, fileName);
model.ProjectId = ProjectId;
model.FileName = fileName;
model.FilePath = path;
model.UploadedBy = User.Identity.Name;
model.UploadedDate = DateTime.UtcNow;
model.ClientId = clientId;
var result = await _project.UploadFiles(model);
file.SaveAs(path1);
}
}
catch (Exception ex)
{
return Json(new { Message = "Error in saving file" });
}
}
}
else
{
return Json(new { Message = "Please Select Project" });
}
return Json(new { Message = "File saved" });
}
以前的代碼開發出了實體框架。藉此創建的實體框架模型我已經看到了,我可以模型文件夾下的EDMX看到類的兩個型號和
namespace Inspinia_MVC5.Entityframework
{
using System;
public partial class ProjectsList_Result
{
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public Nullable<bool> IsActive { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
public string UpdatedBy { get; set; }
public int ClientId { get; set; }
public string Employees { get; set; }
public bool IsAnalyticsStart { get; set; }
public bool IsAnalyticsEnd { get; set; }
public string ReportType { get; set; }
}
}
我必須按照實體框架內以前的方法,無需實體框架模型類型之後
namespace Inspinia_MVC5.Models
{
public class ProjectsModel
{
public int ProjectId { get; set; }
public string ProjectName { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime StartDate { get; set; } = DateTime.Now;
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime EndDate { get; set; } = DateTime.Now;
public bool IsActive { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public string UpdatedBy { get; set; }
public int ClientId { get; set; }
public List<LoginModelViewModel> AllEmployees { get; set; }
public string Employees { get; set; }
public bool IsAnalyticsStart { get; set; }
public bool IsAnalyticsEnd { get; set; }
public List<ReportType> reportTypes { get; set; }
public string ReportType { get; set; }
public string Designation { get; set; }
}
}
在我的Solution文件夾中我看過App_start/Uploads/1/1
。當我運行代碼越來越錯誤
{"Object reference not set to an instance of an object."}
Inner Exception null
請任何人告訴我我做了什麼錯誤?請任何人幫助解決這個問題?
你用調試器上服務器看到什麼是空的?它可能是餅乾?那可能是任何事情! – Heberda
我可以同時使用實體模型和我自己的模型嗎? –
是的,在什麼意義上?你在使用Visual Studio嗎?使用本地主機進行調試應該在引發異常時停止,然後您可以檢查null元素。這是我們需要的信息:) – Heberda