【问题标题】:How to post HttpPostedFileBase with Model data from MVC如何使用 MVC 中的模型数据发布 HttpPostedFileBase
【发布时间】: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


    【解决方案1】:

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

     public ActionResult Create(HttpPostedFileBase file,EmployeeViewModel emp)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多