【问题标题】:string fileName is returning `null`字符串文件名返回 `null`
【发布时间】:2020-06-14 15:13:18
【问题描述】:
public ActionResult Create([Bind(Include = "studentID,Full_Name, DateOfBirth,MembershipID,Contact_Number,Emergency_Name,Emergency_Number,Expiry,Notes,ImagePath,ImageFile")] Student student)
    {
        if (student != null && student.ImageFile != null)
        {
            string fileName = Path.GetFileNameWithoutExtension(student.ImageFile.FileName);
            string extension = Path.GetExtension(student.ImageFile.FileName);
            fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
            student.ImagePath = "~/Image/" + fileName;
            fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
            student.ImageFile.SaveAs(fileName);
        }

        db.Students.Add(student);
        db.SaveChanges();

我的 string fileName 正在返回 null 出于某种原因这正在工作,但我更新了数据库之后它一直在返回 null 这是我的看法

@using (Html.BeginForm("Create", "Students", FormMethod.Post, new { enctype="multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
    @Html.Label("Student Image", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <input type="file" name="ImageFile" />
    </div>
</div>

不知道为什么会发生这种情况,因为我再次重写代码只是为了确定,但它仍然返回null

【问题讨论】:

    标签: asp.net model-view-controller asp.net-mvc-5


    【解决方案1】:

    问题出在我的学生班上 我有

     public HttpPostedFile ImageFile { get; set; }
    

    我应该有的时候

     public HttpPostedFileBase ImageFile { get; set; }
    

    我犯了一个愚蠢的错误

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 2012-10-09
      • 2016-07-25
      • 2014-12-26
      • 2013-08-16
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多