我是asp.net的初學者,並試圖將圖像上載到我的項目Images文件夾中,但未將其上載到所需的文件夾中。有人請給我建議。在文件夾中上傳圖像MVC
Create.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代碼'中看不到任何'Submit'按鈕。 –
請參閱我編輯的問題。 –