[英]Why is my input type="file" always returning null to the viewmodel in ASP.NET MVC and Entity Framework
我有一个使用 post 方法将一些信息发送到视图 model 的页面,它返回除设置为 null 的文件之外的所有值,如此屏幕截图所示:
我试图将byte[]
设置为某个已知长度,但这也无济于事。
这是我认为问题所在的代码部分
<div>
<form asp-controller="ProfilePage" asp-action="UploadProfilePicture" method="post" enctype="multipart/form-data">
<input asp-for="EmployeeNumber" type="hidden" />
<input asp-for="FirstName" type="hidden" />
<input asp-for="LastName" type="hidden" />
<input asp-for="TeamID" type="hidden" />
<input asp-for="Role" type="hidden" />
<input asp-for="SuggestionCount" type="hidden" onchange="fileCheck(this);" />
<input asp-for="ProfilePicture" type="file" onchange="filecheck(this)" />
<input type="Submit" value="Submit" />
</form>
</div>
如果您可能想查看它们,下面是完整的代码页
@model NordicDoorSuggestionSystem.Models.Employees.ProfileViewModel
@{
ViewBag.Title = "Profil § NSS";
Layout = "~/Views/Shared/_ProfileLayout.cshtml";
}
<center>
<div id="cardContainer" class="cardContainer">
<div class="card">
<div class="cardPicture">
<i class="fa fa-user" style="font-size:75px; color: grey;"></i>
<button class="profileUpload" type="button" name="button">Nytt bilde</button>
<@*form asp-controller="ProfilePage" asp-action="UploadProfilePicture" method="get">
<input type="image" name="NewProfilePicture" id="NewImageData" onchange="fileCheck(this);" />
</form>*@
</div>
<div class="cardHeader">
<label>@Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)</label>
</div>
<div class="cardProfileInfo">
<p>Ansatt nr: <label>@Html.DisplayFor(model => model.EmployeeNumber)</label> </p>
<p>Rolle: <label>@Html.DisplayFor(model => model.Role)</label> </p>
<p>Team: <label>@Html.DisplayFor(model => model.TeamName)</label> </p>
</div>
<div>
<form asp-controller="ProfilePage" asp-action="UploadProfilePicture" method="post" enctype="multipart/form-data">
<input asp-for="EmployeeNumber" type="hidden" />
<input asp-for="FirstName" type="hidden" />
<input asp-for="LastName" type="hidden" />
<input asp-for="TeamID" type="hidden" />
<input asp-for="Role" type="hidden" />
<input asp-for="SuggestionCount" type="hidden" onchange="fileCheck(this);" />
<input asp-for="ProfilePicture" type="file" onchange="filecheck(this)" />
<input type="Submit" value="Submit" />
</form>
</div>
<div class="showMyStats">
<center>
<div class="stats">
<div class="present">
<div class="lid">
<span></span>
</div>
<div class="promo">
<p>DU HAR</p>
<h2>@Html.DisplayFor(model => model.SuggestionCount) Forslag!</h2>
</div>
<div class="box">
<span></span>
<span></span>
</div>
</div>
</div>
</center>
</div>
</div>
</div>
</center>
public class ProfilePageController : Controller
{
private readonly UserManager<User> _userManager;
private readonly DataContext _context;
private readonly IEmployeeRepository _employeeRepository;
public ProfilePageController(UserManager<User> userManager, DataContext context, IEmployeeRepository employeeRepository)
{
_userManager = userManager;
_context = context;
_employeeRepository = employeeRepository;
}
// GET: /<controller>/
public async Task<IActionResult> Index()
{
var currentUser = await _userManager.GetUserAsync(HttpContext.User);
ProfileViewModel vm = new ProfileViewModel();
if (currentUser == null)
{
return NotFound();
}
var employee = _employeeRepository.GetEmployeeByNumber(currentUser.EmployeeNumber);
vm.EmployeeNumber = currentUser.EmployeeNumber;
vm.FirstName = currentUser.FirstName;
vm.LastName = currentUser.LastName;
vm.Role = currentUser.Role;
vm.TeamID = employee.TeamID;
var teamname = _context.Team.Where(e => e.TeamID.Equals(employee.TeamID)).Select(e => e.TeamName).FirstOrDefault();
vm.TeamName = teamname;
vm.SuggestionCount = employee.SuggestionCount;
//vm.ProfilePicture = employee.ProfilePicture;
return View(vm);
}
// GET: /<controller>/
public IActionResult Statistic()
{
return View();
}
//byte[] picture ???
[HttpPost]
public async Task<IActionResult> UploadProfilePicture(ProfileViewModel profilevm)
{
//byte[] ImageToByteArray(System.Drawing.Image imageIn)
//{
// using (var ms = new MemoryStream())
// {
// imageIn.Save(ms, imageIn.RawFormat);
// return ms.ToArray();
// }
//}
//var Imgtemp = new byte[16777215];
//Imgtemp[16777215] = ImageToByteArray(profilevm.ProfilePicture);
var employee = new Employee
{
EmployeeNumber = profilevm.EmployeeNumber,
FirstName = profilevm.FirstName,
LastName = profilevm.LastName,
Role = profilevm.Role,
TeamID = profilevm.TeamID,
ProfilePicture = profilevm.ProfilePicture,
SuggestionCount = profilevm.SuggestionCount,
};
_employeeRepository.Update(employee);
return RedirectToAction("Index");
}
}
namespace NordicDoorSuggestionSystem.Models.Employees
{
public class ProfileViewModel
{
public int EmployeeNumber { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? AccountState { get; set; }
public string Role { get; set; }
public byte[]? ProfilePicture { get; set; }
public int? TeamID { get; set; }
public int? SuggestionCount { get; set; }
public string? TeamName { get; set; }
}
}
该代码用于我的第 3 学期 uni 项目
这是存储图片的员工实体部分的截图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.