Introduction:-
in this Example we Explain that How to Insert Update Image in MVC in asp.net in grid view.
Description:-
We can Edit the Image same as we
done in C#.Net but Here we have to manage each code in Diiferant File.Look at
the Following Code in which we can Bind the Image in Gridview usind asp code in
Html Design Form.
For Ex:-
<input type="file" name="file" id="file" /> <%
Response.Write("<img src='../../Content/"+Model.photo+"' alt='ii'
width='80'
height='80' />");
%>
Look at the above
Example we Fetch the Image path like (Model.photo) From the database and Displa are Displaying it
Using Create a <img> tag with src Element and displaying Image at
runtime. So it is very difficult task then as simple display in gridview in
asp.net with c#.
Look also <%=Html.ActionLink("Back
to List",
"Index") %> this will Automatically Create a Link to the
HomePage.
Look the Following code for Fetch Image FileName
QuestionModelDataContext qDB = new QuestionModelDataContext();
var m = qDB.demoimages.First(p => p.id == id);
m.name = collection["name"].ToString();
m.photo = posted.FileName;
to show Example of Insert,Update,Delete in MOdalPopup ModalPopup for CRUD operation
Export Gridview data to PDF file Export Gridview Row to PDF
View Page:-
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<demo.Models.demoimage>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and
try again.") %>
<%-- <% using (Html.BeginForm()) {%>--%>
<% using (Html.BeginForm("Edit", "image",
FormMethod.Post, new { enctype = "multipart/form-data" })){%>
<fieldset>
<legend>Fields</legend>
<%-- <p>
<label
for="id">id:</label>
<%=
Html.TextBox("id", Model.id) %>
<%=
Html.ValidationMessage("id", "*") %>
</p>--%>
<p>
<label for="name">name:</label>
<%= Html.TextBox("name",
Model.name) %>
<%= Html.ValidationMessage("name", "*") %>
</p>
<p visible="false">
<%= Html.TextBox("photo",
Model.photo) %>
</p>
<p>
<label for="photo">photo:</label>
<input type="file" name="file" id="file" /> <%
Response.Write("<img src='../../Content/"+Model.photo+"'
alt='ii' width='80' height='80' />");
%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
//Controller Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using demo.Models;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
using System.Web.UI.HtmlControls;
namespace demo.Controllers
{
public class imageController : Controller
{
//
// GET:
/image/
public ActionResult Index()
{
QuestionModelDataContext qDB = new QuestionModelDataContext();
var a = (from t in qDB.demoimages select t).ToList();
return View(a);
}
//
// GET:
/image/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET:
/image/Create
public ActionResult Create()
{
return View();
}
//
// POST:
/image/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
foreach (string file in Request.Files)
{
HttpPostedFileBase posted =
(HttpPostedFileBase)Request.Files[file];
//
string s= Server.MapPath(@"~/Content/" +
System.IO.Path.GetFileName(posted.FileName));
posted.SaveAs(Server.MapPath(@"~/Content/" + System.IO.Path.GetFileName(posted.FileName)));
QuestionModelDataContext q1
= new
QuestionModelDataContext();
demoimage d = new demoimage();
d.name = collection["name"].ToString();
d.photo = posted.FileName;
q1.demoimages.InsertOnSubmit(d);
q1.SubmitChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET:
/image/Edit/5
public ActionResult Edit(int id)
{
QuestionModelDataContext qDB = new QuestionModelDataContext();
var a = (from t in qDB.demoimages where t.id == id select t).ToList();
return View(a[0]);
}
//
// POST:
/image/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
foreach (string file in Request.Files)
{
HttpPostedFileBase posted =
(HttpPostedFileBase)Request.Files[file];
if (posted.FileName != "")
{
posted.SaveAs(Server.MapPath(@"~/Content/" + System.IO.Path.GetFileName(posted.FileName)));
QuestionModelDataContext qDB = new QuestionModelDataContext();
var m = qDB.demoimages.First(p
=> p.id == id);
m.name = collection["name"].ToString();
m.photo = posted.FileName;
qDB.SubmitChanges();
}
else
{
QuestionModelDataContext qDB = new QuestionModelDataContext();
var m = qDB.demoimages.First(p
=> p.id == id);
m.name = collection["name"].ToString();
m.photo = collection["photo"].ToString();
;
qDB.SubmitChanges();
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
0 comments:
Post a Comment