首页 > 解决方案 > 如何使用 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>

     }

请有人帮助如何在发布时上传带有模型数据的图像。

标签: asp.net-mvc-4

解决方案


您的 Create 方法中的问题是您Post期望 Employee 对象,但您正在EmployeeViewModel与视图绑定,因此使用下面的行可以解决您的问题。

 public ActionResult Create(HttpPostedFileBase file,EmployeeViewModel emp)

推荐阅读