Я пытаюсь загрузить изображение в свою базу данных с помощью кнопки Entity Framework, но когда я выбираю изображение с помощью кнопки и нажимаю кнопку, он перенаправляет меня на выбор файла, и все данные там указаны c текстовое поле есть в текстовом поле, но после нажатия на нее каждый раз отображается только кнопка выбора. Выберите файл или файл не выбран.
Контроллер:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InstituteOfFineArt.Models;
namespace InstituteOfFineArt.Controllers
{
public class StaffController : Controller
{
FINE_ARTSEntities obj = new FINE_ARTSEntities();
// GET: Staff
public ActionResult Staff_pannel()
{
if (Session["userId"] == null)
{
return RedirectToAction("login", "Login");
}
return View();
}
public ActionResult Competition()
{
var data = obj.Competitions.ToList();
return View(data);
}
public ActionResult Create_Competition()
{
return View();
}
[HttpPost]
public ActionResult Create_Competition(Competition a)
{
if (ModelState.IsValid == true)
{
string filename = Path.GetFileNameWithoutExtension(a.ImageFile.FileName);
string extension = Path.GetExtension(a.ImageFile.FileName);
HttpPostedFileBase postedFile = a.ImageFile;
int length = postedFile.ContentLength;
if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")
{
if(length <= 1000000)
{
filename = filename + extension;
a.Image = "~/Images/"+filename;
filename = Path.Combine(Server.MapPath("~/Images/"), filename);
a.ImageFile.SaveAs(filename);
obj.Competitions.Add(a);
int data = obj.SaveChanges();
if(data > 0)
{
TempData["datamessage"] = "<script>alert('Data Inserted Succesfully')</script>";
ModelState.Clear();
return RedirectToAction("Create_Competition","Staff");
}
else
{
TempData["dtemessage"] = "<script>alert('Data Not Inserted')</script>";
}
}
else
{
TempData["lengthmessage"] = "<script>alert('Length should be 10 mb')</script>";
}
}
else
{
TempData["extentionmessage"] = "<script>alert('Format not Supported')</script>";
}
}
return View();
}
public ActionResult Logout()
{
Session.Abandon();
return RedirectToAction("login", "Login");
}
}
}
Класс модели:
[Required(ErrorMessage = "Required")]
[DisplayName("Insert Image")]
public string Image { get; set; }
public HttpPostedFileBase ImageFile { get; set; }
Просмотр:
@using (Html.BeginForm("Create_Competition", "Staff", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Competition</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Conditions, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Conditions, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Conditions, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AwardDetails, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AwardDetails, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AwardDetails, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="ImageFile" required />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-warning form-control" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Competition")
</div>