在mvc中将图像路径插入数据库

时间:2017-08-06 16:18:34

标签: asp.net-mvc

我想将图像插入到Image文件夹中,并使用实体框架将图像路径添加到数据库中。我的模型是,

public class Orphan
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public bool Disable { get; set; }
        public DateTime JoinedDate { get; set; }
        public DateTime? LeaveDate { get; set; }
        public Carer Carer { get; set; }
        public string CarerName { get; set; }
        public string ImagePath { get; set; }


    }

这是视图模型,

public  partial class OrphanViewModel
        {
            [Required]
            [DisplayName("First Name")]
            public string FirstName { get; set; }
            [Required]
            [DisplayName("Last Name")]
            public string LastName { get; set; }
            [Required]
            public int Age { get; set; }
            [Required]
            public string Gender { get; set; }
            [Required]
            public bool Disable { get; set; }
            [Required]
            public string CarerName { get; set; }
            public string ImagePath { get; set; }
            public HttpPostedFileBase ImageFile { get; set; }
        }

这是imagepath的视图。

<div class="form-group">
            @Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" name="ImageFile" required />
            </div>
        </div>

这是控制器

[HttpPost]
 [ValidateAntiForgeryToken]
        public ActionResult Create(OrphanViewModel ovm)
        {
            string fileName = Path.GetFileNameWithoutExtension(ovm.ImageFile.FileName);           

string extension = Path.GetExtension(ovm.ImageFile.FileName);
            fileName = fileName + DateTime.Now.ToString("yymmssff") + extension;
            ovm.ImagePath = "~/Image/" + fileName;
            fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
            ovm.ImageFile.SaveAs(fileName);

            if (ModelState.IsValid)
            {
                db.Orphans.Add(new Orphan()
                {
                    FirstName = ovm.FirstName,
                    LastName = ovm.LastName,
                    Age = ovm.Age,
                    Gender = ovm.Gender,
                    Disable = ovm.Disable,
                    JoinedDate = DateTime.Now,
                    CarerName = ovm.CarerName,
                    ImagePath = ovm.ImagePath
                });
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(ovm);
        }

所以我面临的问题就是在ovm.ImageFile {&#34;对象引用没有设置为对象的实例。&#34;} .HELP

string fileName = Path.GetFileNameWithoutExtension(ovm.ImageFile.FileName);  

1 个答案:

答案 0 :(得分:1)

您需要在前端定义表单内容类型 - enctype =“multipart / form-data”

<form method="post" enctype="multipart/form-data" action="/ActionPath">