C# 在mvc中从数据库读取后为空值
我的博客控制器中有编辑视图和编辑操作。 在我用“创建操作”创建了一篇文章并将图像上传到数据库文件夹后,我更新了post.postmage上的路径(字符串值)。 我可以看到文件夹中的图像,可以看到图像的路径,也可以在编辑视图中看到图片的预览。在我的数据库中,它保存为(~/Images/PostID/PictureName)。但是在我编辑我的帖子之后,我想做一个复选框,如果选中,我可以编辑图片,如果不选中,我就删除图片。我发送参数,我的问题是,在调试器上,我看到“字符串positmage”为null,但在数据库表上它有路径! 因为所有这些都不起作用,我不关心逻辑,为什么它是空的???? 这是我的密码: 视图:C# 在mvc中从数据库读取后为空值,c#,model-view-controller,view,null,edit,C#,Model View Controller,View,Null,Edit,我的博客控制器中有编辑视图和编辑操作。 在我用“创建操作”创建了一篇文章并将图像上传到数据库文件夹后,我更新了post.postmage上的路径(字符串值)。 我可以看到文件夹中的图像,可以看到图像的路径,也可以在编辑视图中看到图片的预览。在我的数据库中,它保存为(~/Images/PostID/PictureName)。但是在我编辑我的帖子之后,我想做一个复选框,如果选中,我可以编辑图片,如果不选中,我就删除图片。我发送参数,我的问题是,在调试器上,我看到“字符串positmage”为null
渲染后期图像字段
@Html.EditorFor(model => model.PostImage, new { htmlAttributes = new { @class = "form-control" } })
因此,从我最近读到的内容来看,不要将实体框架模型直接传递到视图中。创建一个单独的
ViewModel
。在GET
上,根据您的EF模型构建此ViewModel
,在POST
上,从ViewModel
中提取所需信息,并更新数据库中的EF模型
在您看来,图像URL没有EditorFor()
或HiddenFor()
,这就是为什么在POST
上它将为空
这正是您希望使用
ViewModels
而不是在视图中直接使用EF模型的原因,这样您就可以拥有一个单独的视图模型,其中只包含需要编辑/更新/显示的属性,而需要保持不变的实体的属性将保持不变。其中,在所有代码中,是实际的空值吗?您希望在何处填充该值?我在postimage上编写了,但在渲染后用@Html.EditorFor(model=>model.postimage,new{htmlAttributes=new{@class=“form control”})修复了它
// GET: Blog/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Post post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
return View(post);
}
// POST: Blog/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PostID,PostTitle,PostAuthor,WebSite,PostDate,PostText,PostImage,PostVideo")] Post post, HttpPostedFileBase file, bool checkImage)
{
var fileName = "";
if (ModelState.IsValid)
{
db.Entry(post).State = EntityState.Modified;
if (checkImage == true)
{
//Check if there is a file
if (file != null && file.ContentLength > 0)
{
//Check if there is an image
var supportedTypes = new[] { "jpg", "jpeg", "gif", "png" };
var fileExt = System.IO.Path.GetExtension(file.FileName).Substring(1);
if (!supportedTypes.Contains(fileExt))
{
ViewBag.Message = "Invalid image type. Only the following types (jpg, jpeg, gif, png) are supported";
return View();
}
//Check if there is a file on database
if ( !(String.IsNullOrEmpty(post.PostImage)) )
{
//Delete old file in folder
System.IO.File.Delete(post.PostImage);
//Save new file in folder
var folder = Path.Combine(Server.MapPath("~/Images/"), Convert.ToString(post.PostID));
var path = Path.Combine(folder, fileName);
file.SaveAs(path);
//Save path in database
string targetPath = String.Concat("~/Images/", Convert.ToString(post.PostID), "/", fileName);
post.PostImage = targetPath;
}
//No file in database
else
{
var folder = Path.Combine(Server.MapPath("~/Images/"), Convert.ToString(post.PostID));
var path = Path.Combine(folder, fileName);
file.SaveAs(path);
//Save path in database
string targetPath = String.Concat("~/Images/", Convert.ToString(post.PostID), "/", fileName);
post.PostImage = targetPath;
}
}
//Checkbox is checked but not file uploaded
else
ViewBag.Message = "Checkbox is checked, please upload an image";
return View();
}
else
{
//Checkbox is not checked - Delete the image from database
if( !(String.IsNullOrEmpty(post.PostImage)) )
{
//Delete old file in folder
try
{
System.IO.File.Delete("\a.txt");
post.PostImage = null;
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(post);
}
@Html.EditorFor(model => model.PostImage, new { htmlAttributes = new { @class = "form-control" } })