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
Raju Fodse
1.1k
244
31.5k
Update Image in MVC
Dec 7 2019 1:50 AM
I have table named "Contacts" in this table I want to update PhotoID Filed so as new file will uploaded and path will replace in database against record. I done following Code for Controller and Edit View but I Get erro
Object reference not set to an instance of an object.
[HttpGet]
public
ActionResult EditContactProfile(
int
id)
{
if
(id == 0)
{
return
new
HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Contact contact = db.Contacts.Find(id);
if
(contact ==
null
)
{
return
HttpNotFound();
}
return
View(contact);
}
[HttpPost]
public
ActionResult EditContactProfile(Contact contact, HttpPostedFileBase ImageFile)
{
if
(ModelState.IsValid)
{
if
(ImageFile.ContentLength>0)
{
string
fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);
string
extension = Path.GetExtension(ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString(
"yymmssfff"
) + extension;
contact.PhotoID =
"~/Uploads/UploadImages/"
+ fileName;
fileName = Path.Combine(Server.MapPath(
"~/Uploads/UploadImages/"
), fileName);
ImageFile.SaveAs(fileName);
}
db.Entry(contact).State = EntityState.Modified;
db.SaveChanges();
return
RedirectToAction(
"IndexContacts"
);
}
return
View(contact);
}
And View is
@model EasyES.Models.Contact
@{
ViewBag.Title
=
"EditContact"
;
}
<
head
>
<
link
href
=
"~/Content/bootstrap.min.css"
rel
=
"stylesheet"
/>
<
script
src
=
"~/Scripts/jquery-3.3.1.min.js"
>
</
script
>
<
script
src
=
"~/Scripts/bootstrap.min.js"
>
</
script
>
<
script
src
=
"~/Scripts/jquery-ui-1.12.1.js"
>
</
script
>
<
style
>
input,
select,
textarea {
max-width: 100%; /*280px; Dafult Size*/
}
</
style
>
</
head
>
<
body
>
<
div
class
=
"EasyViewDiv"
>
<
div
class
=
"form-group"
>
<
div
class
=
"row"
>
<
div
class
=
"col-md-6"
>
<
h1
style
=
"color:#808080"
>
Edit Contact Profile Photo
</
h1
>
</
div
>
<
div
class
=
"col-md-6"
>
</
div
>
</
div
>
</
div
>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(
a
=
>
a.ContactID)
@Html.HiddenFor(
a
=
>
a.Name)
@Html.HiddenFor(
a
=
>
a.NickName)
@Html.HiddenFor(
a
=
>
a.ComapnyName)
@Html.HiddenFor(
a
=
>
a.Department)
@Html.HiddenFor(
a
=
>
a.Designation)
@Html.HiddenFor(
a
=
>
a.Email1)
@Html.HiddenFor(
a
=
>
a.Email2)
@Html.HiddenFor(
a
=
>
a.Contact1)
@Html.HiddenFor(
a
=
>
a.Contact2)
@Html.HiddenFor(
a
=
>
a.Contact3)
@Html.HiddenFor(
a
=
>
a.AddressHome)
@Html.HiddenFor(
a
=
>
a.AddressWorks)
@Html.HiddenFor(
a
=
>
a.WebAddress)
@Html.HiddenFor(
a
=
>
a.KeyWord)
@Html.HiddenFor(
a
=
>
a.Status)
<
div
class
=
"row"
>
<
div
class
=
"col-md-6"
>
<
div
class
=
"form-group"
>
@Html.ValidationSummary(true, "", new { @
class
=
"text-danger"
})
<
div
class
=
"form-group"
>
@Html.LabelFor(
model
=
>
model.Name, "Contact Name", htmlAttributes: new { @
class
=
"control-label col-md-12"
})
<
div
class
=
"form-group col-md-12"
>
@Html.EditorFor(
model
=
>
model.Name, new {
htmlAttributes
=
new
{ @
class
=
"form-control"
} })
@Html.ValidationMessageFor(
model
=
>
model.Name, "", new { @
class
=
"text-danger"
})
</
div
>
</
div
>
</
div
>
</
div
>
<
div
class
=
"col-md-6"
>
<
div
class
=
"form-group"
>
@Html.ValidationSummary(true, "", new { @
class
=
"text-danger"
})
<
div
class
=
"form-group"
>
@Html.LabelFor(
model
=
>
model.PhotoID, "UPload Photo", htmlAttributes: new { @
class
=
"control-label col-md-12"
})
<
div
class
=
"form-group col-md-12"
>
<
div
class
=
"form-group col-md-9"
>
<
input
type
=
"file"
id
=
"FileStorage"
name
=
"ImageFile"
/>
<
p
class
=
"text-danger"
>
Only PNG file with 500 kb
</
p
>
</
div
>
</
div
>
</
div
>
</
div
>
</
div
>
</
div
>
<
div
align
=
"center"
class
=
"container"
>
<
button
type
=
"submit"
value
=
"save"
class
=
"btn-outline-success ESbutton button3"
>
<
span
class
=
"fa fa-save"
>
</
span
>
</
button
>
<
button
title
=
"List View"
class
=
"btn-outline-info ESbutton button3"
onclick
="location.href='@Url.Action("IndexContacts", "Contacts
")';return false;"
>
<
span
class
=
"fa fa-list"
>
</
span
>
</
button
>
</
div
>
}
</
div
>
</
body
>
Reply
Answers (
5
)
Upload Default File Path if field is empty
Retrive data from sql and asp.net mvc and angular