【发布时间】:2019-07-28 15:23:20
【问题描述】:
如何使用 HttpPostedFileBase 和其他 3 个模型数据上传文件 并将其保存到数据库。当我尝试保存数据员工模型值时 变为空并给出对象引用错误。我有一个创建视图链接 获取页面上的新员工。我收到对象引用错误时 尝试保存。
EmployeeModelClass.cs
public class EmployeeViewModel
{
public Employee Employee { get; set; }
public HttpPostedFileBase File { get; set; }
public List<Employee> EmployeeList { get; set; }
public IEnumerable<Region> Regions { get;set; }
public IEnumerable<City> Cities { get; set; }
}
员工等级
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; }
[DataType(DataType.DateTime)]
public DateTime? BirthDate { get; set; }
[Display(Name = "Region")]
public int RegionId { get; set; }
public City City { get; set; }
[Display(Name = "City")]
public int CityId { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
public string Confirm Password { get; set; }
public int ImageSize { get; set; }
public string FileName { get; set; }
public byte[] ImageData { get; set; }
}
获取创建请求。这里我从 Get.chtml 视图重定向。
[HttpGet]
public ActionResult Create()
{
var viewmodel = new EmployeeViewModel
{
Regions = _context.Regions.ToList(),
Cities = _context.Cities.ToList(),
};
return View("Create", viewmodel);
}
发布创建请求。这里我的员工模型值变为空,提交时给出对象引用空错误。
[HttpPost]
public ActionResult Create(HttpPostedFileBase file,Employee emp)
{
}
创建.chtml
@model EmployeeManagement.ViewModel.EmployeeViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (@Html.BeginForm("Create", "Employee", FormMethod.Post, new {
enctype = "multipart/form-data" }))
{
<div class = "form-group">
@Html.LabelFor(m => m.Employee.Name)
@Html.TextBoxFor(m => m.Employee.Name, new { @class = "form-control"
})
</div>
<div class = "form-group">
@Html.LabelFor(m => m.Employee.BirthDate)
@Html.TextBoxFor(m => m.Employee.BirthDate, new { @class = "form-
control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Employee.RegionId)
@Html.DropDownListFor(m => m.Employee.RegionId, new
SelectList(Model.Regions, "RegionId", "RegionName"), "Please Select
Region", new { @onchange = "BindCity()", @class = "form-control" })
@Html.ValidationMessageFor(m => m.Employee.RegionId)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Employee.CityId)
@Html.DropDownListFor(m => m.Employee.CityId, new
SelectList(Model.Cities, "CityId", "CityName"), "Please Select
City", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Employee.CityId)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Employee.Password)
@Html.PasswordFor(m => m.Employee.Password, new { @class = "form-
control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Employee.ConfirmPassword)
@Html.PasswordFor(m => m.Employee.ConfirmPassword, new { @class =
"form-control" })
</div>
<div>
@Html.TextBoxFor(m=>m.File, new { type = "file",name="File" })
@Html.ValidationMessageFor(m=>m.File.FileName)
</div>
<p><button type="submit" class="btn btn-primary">Save</button></p>
}
请有人帮助如何上传带有模型数据的图像 发帖。
【问题讨论】:
标签: asp.net-mvc-4