Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.Data.Entity;
- namespace AjaxFormMVC.Models
- {
- public class Actor {
- public int ActorID { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- [DataType(DataType.ImageUrl)]
- public string Image { get; set; }
- }
- public class ActorDbContext : DbContext
- {
- public DbSet<Actor> Actors { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using AjaxFormMVC.Models;
- using System.IO;
- namespace AjaxFormMVC.Controllers
- {
- public class HomeController : Controller
- {
- ActorDbContext db = new ActorDbContext();
- public ActionResult Index()
- {
- return View(db.Actors.ToList());
- }
- public ActionResult Create()
- {
- return PartialView("_Actor");
- }
- [HttpPost]
- public ActionResult Create(HttpPostedFileBase imageFile, Actor model, string cmd)
- {
- try
- {
- if (imageFile != null && imageFile.ContentLength > 0)
- {
- var fileName = Path.GetFileName(imageFile.FileName);
- if (cmd == "Create")
- {
- model.Image = fileName;
- db.Actors.Add(model);
- db.SaveChanges();
- }
- else
- {
- var actor = db.Actors.Where(m => m.ActorID == model.ActorID).FirstOrDefault();
- if (actor != null)
- {
- actor.Image = fileName;
- db.SaveChanges();
- }
- }
- var path = Path.Combine(Server.MapPath("~/Content/Actors/"), model.ActorID.ToString());
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- imageFile.SaveAs(path + "/" + fileName);
- }
- }
- catch { }
- return RedirectToAction("Index");
- }
- public ActionResult Edit(int id)
- {
- var actor = db.Actors.Where(m => m.ActorID == id).FirstOrDefault();
- ViewBag.IsUpdate = true;
- return PartialView("_Actor", actor);
- }
- public ActionResult Delete(int id)
- {
- var actor = db.Actors.FirstOrDefault(m => m.ActorID == id);
- if (actor != null)
- {
- db.Actors.Remove(actor);
- db.SaveChanges();
- }
- return RedirectToAction("Index");
- }
- public ActionResult GetActorList()
- {
- return PartialView("_ActorList", db.Actors.ToList());
- }
- public ActionResult About()
- {
- ViewBag.Message = "Your app description page.";
- return View();
- }
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
- }
- }
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- @model AjaxFormMVC.Models.Actor
- <script type="text/javascript">
- $(document).ready(function () {
- $('#frmCreateActor').ajaxForm(function () {
- $('#modalDialog').dialog("close");
- $.post('@Url.Action("GetActorList", "Home")', function (data) {
- $("#actorList").html(data);
- });
- })
- });
- </script>
- @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @id = "frmCreateActor", @enctype = "multipart/form-data" }))
- {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>Actor</legend>
- <div class="editor-label">
- @Html.LabelFor(model => model.Name)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Name)
- @Html.ValidationMessageFor(model => model.Name)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Description)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Description)
- @Html.ValidationMessageFor(model => model.Description)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Image)
- </div>
- <div class="editor-field">
- <input type="file" name="imageFile" id="imageFile" />
- @Html.ValidationMessageFor(model => model.Image)
- </div>
- <p>
- @if (ViewBag.IsUpdate !=null && ViewBag.IsUpdate)
- {
- @Html.HiddenFor(model => model.ActorID);
- <input name="cmd" type="submit" value="Update" />
- }
- else
- {
- <input name="cmd" type="submit" value="Create" />
- }
- <input type="button" value="Cancel" id="btncancel" />
- </p>
- </fieldset>
- }
- @model IEnumerable<AjaxFormMVC.Models.Actor>
- <p>
- @Html.ActionLink("Create New", "Create", null, new { @class = "createActor", title = "Create Actor" })
- </p>
- <table style="border: 1px solid black;" cellpadding="10">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Description)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Image)
- </th>
- <th>
- </th>
- </tr>
- @foreach (var item in Model)
- {
- <tr>
- <td valign="top">
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td valign="top">
- @Html.DisplayFor(modelItem => item.Description)
- </td>
- <td valign="top">
- <img src="@Url.Content("~/Content/Actors/" + item.ActorID + "/" + item.Image + "")" alt="@item.Name" width="100px" height="100px" />
- </td>
- <td valign="top">
- @Html.ActionLink("Edit", "Edit", new { id = item.ActorID }, new { @class = "createActor", title = "Edit Actor" })
- |
- @Html.ActionLink("Delete", "Delete", new { id = item.ActorID })
- </td>
- </tr>
- }
- </table>
- @{
- ViewBag.Title = "Home Page";
- }
- @model IEnumerable<AjaxFormMVC.Models.Actor>
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- <link href="@Url.Content("~/Content/themes/base/minified/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
- <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/form_ajax.js")" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- $.ajaxSetup({ cache: false });
- $(".createActor").live("click", function (e) {
- var $url = $(this).attr('href');
- var $title = $(this).attr('title');
- var $dialog = $('<div id="modalDialog"></div>');
- $dialog.empty();
- $dialog.load($url).dialog({
- autoOpen: false,
- title: $title,
- resizable: false,
- height: 300,
- width: 380,
- show: { effect: 'drop', direction: "up" },
- modal: true,
- draggable: true
- });
- $dialog.dialog('open');
- return false;
- });
- $("#btncancel").live("click", function (e) {
- $("#modalDialog").dialog('close');
- });
- });
- </script>
- </head>
- <body>
- <div id="actorList">
- @Html.Partial("_ActorList", Model)
- </div>
- </body>
- </html>
- public class Student
- {
- public int id { get; set; }
- [DataType(DataType.ImageUrl)]
- public string Image { get; set; }
- }
- public class StudentrDbContext : DbContext
- {
- public DbSet<Student> Student { get; set; }
- }
- public string ImageContentType {get;set;}
- public byte[] Image {get;set;}
- public class Student
- {
- public int id { get; set; }
- public string ImageContentType {get;set;}
- public byte[] Image {get;set;}
- }
- [HttpPost]
- public ActionResult Create(ApplicationViewModel vm, HttpPostedFileBase Image)
- {
- if (ModelState.IsValid)
- {
- if (Image != null)
- {
- vm.Application.ImageMimeType = Image.ContentType;
- vm.Application.Image = new byte[Image.ContentLength];
- Image.InputStream.Read(vm.Application.Image, 0, Image.ContentLength);
- }
- try
- {
- // TODO: Add save logic here
- _provider.SaveApplication(vm);
- TempData["success"] = true;
- TempData["message"] = String.Format("Sucessfully added {0}", vm.Application.ApplicationName);
- }
- catch (Exception)
- {
- TempData["error"] = true;
- TempData["message"] = String.Format("Unsucessfully Added {0}", vm.Application.ApplicationName);
- }
- return RedirectToAction("Index");
- }
- vm = _provider.ReInitialiseVm(vm);
- return View("Shape", vm);
- }
- vm.Application.ImageMimeType = Image.ContentType;
- vm.Application.Image = new byte[Image.ContentLength];
- Image.InputStream.Read(vm.Application.Image, 0, Image.ContentLength);
- <td>
- @if (item.Image == null)
- {
- @:None
- } else {
- <img width="48" src="data:@item.ImageMimeType;base64,@(Convert.ToBase64String(item.Image))" />
- }
- </td>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement