TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Lujain Mohammad
NA
10
382
Inserting Image Error!
Mar 12 2020 3:57 PM
hello all,
i tried to upload image to database and then retrieve it from database,
when i tried to upload it, the webapp is stop debugging and didn't upload the image
here's my model
public
class
Drink
{
public
int
DrinkId {
get
;
set
; }
[Display(Name =
"Drink Name"
)]
public
string
DrinkName {
get
;
set
; }
[Display(Name =
"Company Name"
)]
public
string
DrinkCompany {
get
;
set
; }
[Display(Name =
"Certificate"
)]
public
string
CertificateDrinkUrl {
get
;
set
; }
[NotMapped]
public
HttpPostedFileBase FileBase {
get
;
set
; }
}
and here's my controller
public
class
DrinkController : Controller
{
IRepositoryBase
drinks;
public
DrinkController(IRepositoryBase
drinks)
{
this
.drinks = drinks;
}
// GET: Drink
public
ActionResult Index()
{
return
View();
}
[HttpGet]
public
ActionResult AddDrink()
{
var model =
new
Drink();
return
View(model);
}
[HttpPost]
public
ActionResult AddDrink(Drink drink)
{
string
fileName = Path.GetFileNameWithoutExtension(drink.FileBase.FileName);
string
extension = Path.GetExtension(drink.FileBase.FileName);
fileName = fileName + DateTime.Now.ToString(
"yymmssffff"
) + extension;
drink.CertificateDrinkUrl =
"~/Images/"
+ fileName;
fileName = Path.Combine(Server.MapPath(
"~/Images/"
), fileName);
drink.FileBase.SaveAs(fileName);
using
(DataContext db =
new
DataContext())
{
db.Drinks.Add(drink);
db.SaveChanges();
}
ModelState.Clear();
return
View();
}
and finally here's my view
@model WorldOfHalal.Models.Drink
@{
ViewBag.Title
=
"AddDrink"
;
}
<
h2
>
Add Drink
</
h2
>
@using (Html.BeginForm("AddDrink", "Drink", FormMethod.Post, new {
enctype
=
"multipart/form-data"
}))
{
@Html.AntiForgeryToken()
<
div
class
=
"form-horizontal"
>
<
hr
/>
@Html.ValidationSummary(true, "", new { @
class
=
"text-danger"
})
<
div
class
=
"form-group"
>
@Html.LabelFor(
model
=
>
model.DrinkName, htmlAttributes: new { @
class
=
"control-label col-md-2"
})
<
div
class
=
"col-md-10"
>
@Html.EditorFor(
model
=
>
model.DrinkName, new {
htmlAttributes
=
new
{ @
class
=
"form-control"
} })
@Html.ValidationMessageFor(
model
=
>
model.DrinkName, "", new { @
class
=
"text-danger"
})
</
div
>
</
div
>
<
div
class
=
"form-group"
>
@Html.LabelFor(
model
=
>
model.DrinkCompany, htmlAttributes: new { @
class
=
"control-label col-md-2"
})
<
div
class
=
"col-md-10"
>
@Html.EditorFor(
model
=
>
model.DrinkCompany, new {
htmlAttributes
=
new
{ @
class
=
"form-control"
} })
@Html.ValidationMessageFor(
model
=
>
model.DrinkCompany, "", new { @
class
=
"text-danger"
})
</
div
>
</
div
>
<
div
class
=
"form-group"
>
@Html.LabelFor(
model
=
>
model.CertificateDrinkUrl, htmlAttributes: new { @
class
=
"control-label col-md-2"
})
<
div
class
=
"col-md-10"
>
<
input
type
=
"file"
name
=
"FileBase"
required
=
""
/>
</
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", "DrinkList", null, new {@
class
=
"btn btn-primary"
})
</
div
>
could anyone help me please?
Reply
Answers (
3
)
Encrypt And Decrypt Connection String In ASP.NET core 2.2
I want to review posted data before final review