如何在网站中插入图片 (Asp.net mvc 5)
How to insert image in website (Asp.net mvc 5)
我想让用户从他们的电脑上选择一张图片并将其上传到我的网站,但是它没有打开选择图片的浏览选项和 return 到列表视图。
这是我的代码:
[HttpPost]
public ActionResult AddRestuarantItem(RestaurantItem restaurantItem, HttpPostedFileBase certificateImage)
{
if (ModelState.IsValid)
{
try
{
if (certificateImage != null)
{
string path = Path.Combine(Server.MapPath("~/UploadedFiles"),
Path.GetFileName(certificateImage.FileName));
certificateImage.SaveAs(path);
}
ViewBag.FileStatus = "Image uploaded successfully";
}
catch (Exception)
{
ViewBag.FileStatus = "Error while image uploading.";
}
}
restaurantItems.Insert(restaurantItem);
restaurantItems.Commit();
return RedirectToAction("RestaurantItemList");
}
这是我的观点:
@model WorldOfHalal.Models.RestaurantItem
@{
ViewBag.Title = "AddRestuarantItem";
}
<h2>AddRestuarantItem</h2>
@using (Html.BeginForm("AddRestuarantItem", "Restaurant", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>RestaurantItem</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ItemName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ItemName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CertificateImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CertificateImageUrl, new { htmlAttributes = new { @class = "form-control", @type = "CertificateImageUrl" } })
@Html.ValidationMessageFor(model => model.CertificateImageUrl, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="image" value="Upload" class="btn btn-primary" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 text-success">
@ViewBag.FileStatus
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
您必须输入 type="file" 才能上传图片,如下所示:
<input type="file" name="certificateImage"/>
我想让用户从他们的电脑上选择一张图片并将其上传到我的网站,但是它没有打开选择图片的浏览选项和 return 到列表视图。
这是我的代码:
[HttpPost]
public ActionResult AddRestuarantItem(RestaurantItem restaurantItem, HttpPostedFileBase certificateImage)
{
if (ModelState.IsValid)
{
try
{
if (certificateImage != null)
{
string path = Path.Combine(Server.MapPath("~/UploadedFiles"),
Path.GetFileName(certificateImage.FileName));
certificateImage.SaveAs(path);
}
ViewBag.FileStatus = "Image uploaded successfully";
}
catch (Exception)
{
ViewBag.FileStatus = "Error while image uploading.";
}
}
restaurantItems.Insert(restaurantItem);
restaurantItems.Commit();
return RedirectToAction("RestaurantItemList");
}
这是我的观点:
@model WorldOfHalal.Models.RestaurantItem
@{
ViewBag.Title = "AddRestuarantItem";
}
<h2>AddRestuarantItem</h2>
@using (Html.BeginForm("AddRestuarantItem", "Restaurant", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>RestaurantItem</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ItemName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ItemName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CertificateImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CertificateImageUrl, new { htmlAttributes = new { @class = "form-control", @type = "CertificateImageUrl" } })
@Html.ValidationMessageFor(model => model.CertificateImageUrl, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="image" value="Upload" class="btn btn-primary" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 text-success">
@ViewBag.FileStatus
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
您必须输入 type="file" 才能上传图片,如下所示:
<input type="file" name="certificateImage"/>