asp.net-mvc-4 - 如何使用 MVC 中的模型数据发布 HttpPostedFileBase
问题描述
如何使用 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>
}
请有人帮助如何在发布时上传带有模型数据的图像。
解决方案
您的 Create 方法中的问题是您Post
期望 Employee 对象,但您正在EmployeeViewModel
与视图绑定,因此使用下面的行可以解决您的问题。
public ActionResult Create(HttpPostedFileBase file,EmployeeViewModel emp)
推荐阅读
- laravel - Laravel - 名称而不是 ID
- javascript - 仅当用户使用 Google chrome 时如何打开网页
- php - 在 WooCommerce 中的相关产品上显示同一类别之前的交叉销售
- javascript - 如何让我的 AJAX 响应通过点击事件?
- python - 使用flask_restful的Python单元测试运行错误
- jsp - 对于大量记录,从 DB 到 excel 的 JSP 输出流非常慢
- c++ - 使用带有 std::function 的放置 new 不起作用
- objective-c - UIFocusGuide:仅在系统无解时转发
- c# - 如何使用 Rx.Net 处理可能是突发或垃圾邮件的事件?
- python - ValueError: 操作数不能与形状 (4,) (2,) 一起广播