我在我的博客控制器中有编辑视图和编辑操作。 当我用“创建动作”创建了一篇文章后,我将图像上传到数据库文件夹后,我更新了post.PostImage(字符串值)上的路径。 我可以在文件夹中看到图像,并且可以看到图像的路径,并且还可以在编辑视图中看到图片的预览。在我的数据库中它保存为(〜/ Images/PostID/PictureName)。但是当我编辑我的文章后,我想创建一个复选框,如果选中,我可以编辑图片,何时不检查我删除图片。我发送的参数,我的问题是,在调试器,我看到“字符串postimage”为空,但在数据库表上它有路径! 因为这一切都不起作用,我不关心逻辑,为什么它是空的? 这里是我的代码:从mvc中的数据库读取后的空值
VIEW:
@model WebApplication1.Models.Post
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm(Html.BeginForm("Edit", "Blog", FormMethod.Post, new { enctype = "multipart/form-data" })))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Post</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PostID)
<div class="form-group">
@Html.LabelFor(model => model.PostTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostAuthor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostAuthor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WebSite, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WebSite, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WebSite, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostText, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostText, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostText, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
<b>Upload image:</b>
@if (!Model.PostImage.IsEmpty())
{
@Html.CheckBox("checkImage", true)
<img src="@Url.Content(Model.PostImage)" alt="@Model.PostAuthor" width="300" />
}
else
{
@Html.CheckBox("checkImage", false)
}
</div>
<input type="file" name="file" id="file" />
<!-- Show message of the controller -->
@ViewBag.Message
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostVideo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostVideo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostVideo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to Posts List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
编辑动作在博客控制器:
// 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);
}
其中,在所有的代码中,是实际的空值?你认为这个价值会被填充在哪里? – David
我在postimage上写道,但是我在渲染后修复了它:@ Html.EditorFor(model => model.PostImage,new {htmlAttributes = new {@class =“form-control”}}) – Alex