【发布时间】:2017-07-06 09:41:48
【问题描述】:
我是 asp.net 的初学者并尝试将图像上传到我的项目图像文件夹中,但它没有将其上传到所需的文件夹中。任何人请给我建议。
创建.cshtml
@using (Html.BeginForm("Create", "Lenses", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>lens</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.lens_img, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="file" id="file" style="width: 100%;" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Controller.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "lens_img")] lens lens, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/")
+ file.FileName);
lens.lens_img = file.FileName;
}
db.lenses.Add(lens);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(lens);
}
【问题讨论】:
-
我在您的
Razor code中看不到任何Submit按钮。 -
请看我编辑的问题。
标签: asp.net asp.net-mvc asp.net-mvc-4