我是 ASP.NET 的新手,如果有人能提供帮助,我将不胜感激。我有我的文件输入:
<form action="NewProject.cshtml" method="post" enctype="multipart/form-data">
<fieldset>
<legend> Upload Image </legend>
<label for="Image">Image</label>
<input type="file" name="Image" id ="filename"/>
<br/>
<input type="submit" value="Upload" id="Add" />
</fieldset>
要上传图片,我使用:
@{ WebImage photo = null;
var newFileName = "";
var imagePath = "";
if(IsPost){
photo = WebImage.GetImageFromRequest();
if(photo != null){
newFileName = Guid.NewGuid().ToString() + "_" +
Path.GetFileName(photo.FileName);
imagePath = @"Images\" + newFileName;
photo.Save(@"~\" + imagePath);
//an attempt
PoleInvestProject.Models.Project project = new PoleInvestProject.Models.Project();
project.Image = imagePath; //storing in model property Image
}
}
}
现在我需要从那里获取图像的路径并将其与具有属性的模型相关联public string Image { get; set; }
。我想将此文件路径存储在我的数据库中DBContext context
。
context.Projects.Add(new Project()
{
Image = model.Image
});
context.SaveChanges();
但这给了我 NULL,我的数据库表中没有Projects
. 我究竟做错了什么?提前致谢!