Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Data.Entity;
  7.  
  8. namespace AjaxFormMVC.Models
  9. {
  10. public class Actor {
  11. public int ActorID { get; set; }
  12. public string Name { get; set; }
  13. public string Description { get; set; }
  14.  
  15. [DataType(DataType.ImageUrl)]
  16. public string Image { get; set; }
  17. }
  18. public class ActorDbContext : DbContext
  19. {
  20. public DbSet<Actor> Actors { get; set; }
  21. }
  22. }
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Web;
  28. using System.Web.Mvc;
  29. using AjaxFormMVC.Models;
  30. using System.IO;
  31.  
  32. namespace AjaxFormMVC.Controllers
  33. {
  34. public class HomeController : Controller
  35. {
  36.  
  37. ActorDbContext db = new ActorDbContext();
  38. public ActionResult Index()
  39. {
  40. return View(db.Actors.ToList());
  41. }
  42.  
  43. public ActionResult Create()
  44. {
  45. return PartialView("_Actor");
  46. }
  47.  
  48. [HttpPost]
  49. public ActionResult Create(HttpPostedFileBase imageFile, Actor model, string cmd)
  50. {
  51. try
  52. {
  53. if (imageFile != null && imageFile.ContentLength > 0)
  54. {
  55. var fileName = Path.GetFileName(imageFile.FileName);
  56. if (cmd == "Create")
  57. {
  58. model.Image = fileName;
  59. db.Actors.Add(model);
  60. db.SaveChanges();
  61. }
  62. else
  63. {
  64. var actor = db.Actors.Where(m => m.ActorID == model.ActorID).FirstOrDefault();
  65. if (actor != null)
  66. {
  67. actor.Image = fileName;
  68. db.SaveChanges();
  69. }
  70. }
  71.  
  72. var path = Path.Combine(Server.MapPath("~/Content/Actors/"), model.ActorID.ToString());
  73. if (!Directory.Exists(path))
  74. Directory.CreateDirectory(path);
  75. imageFile.SaveAs(path + "/" + fileName);
  76. }
  77. }
  78. catch { }
  79.  
  80. return RedirectToAction("Index");
  81. }
  82.  
  83. public ActionResult Edit(int id)
  84. {
  85. var actor = db.Actors.Where(m => m.ActorID == id).FirstOrDefault();
  86. ViewBag.IsUpdate = true;
  87. return PartialView("_Actor", actor);
  88. }
  89.  
  90. public ActionResult Delete(int id)
  91. {
  92. var actor = db.Actors.FirstOrDefault(m => m.ActorID == id);
  93. if (actor != null)
  94. {
  95. db.Actors.Remove(actor);
  96. db.SaveChanges();
  97. }
  98. return RedirectToAction("Index");
  99. }
  100.  
  101. public ActionResult GetActorList()
  102. {
  103. return PartialView("_ActorList", db.Actors.ToList());
  104. }
  105.  
  106. public ActionResult About()
  107. {
  108. ViewBag.Message = "Your app description page.";
  109.  
  110. return View();
  111. }
  112.  
  113. public ActionResult Contact()
  114. {
  115. ViewBag.Message = "Your contact page.";
  116.  
  117. return View();
  118. }
  119. }
  120. }
  121.  
  122. @{
  123. Layout = null;
  124. }
  125.  
  126. <!DOCTYPE html>
  127.  
  128. @model AjaxFormMVC.Models.Actor
  129. <script type="text/javascript">
  130. $(document).ready(function () {
  131. $('#frmCreateActor').ajaxForm(function () {
  132.  
  133. $('#modalDialog').dialog("close");
  134.  
  135. $.post('@Url.Action("GetActorList", "Home")', function (data) {
  136. $("#actorList").html(data);
  137. });
  138. })
  139. });
  140. </script>
  141. @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @id = "frmCreateActor", @enctype = "multipart/form-data" }))
  142. {
  143. @Html.ValidationSummary(true)
  144.  
  145. <fieldset>
  146. <legend>Actor</legend>
  147. <div class="editor-label">
  148. @Html.LabelFor(model => model.Name)
  149. </div>
  150. <div class="editor-field">
  151. @Html.EditorFor(model => model.Name)
  152. @Html.ValidationMessageFor(model => model.Name)
  153. </div>
  154. <div class="editor-label">
  155. @Html.LabelFor(model => model.Description)
  156. </div>
  157. <div class="editor-field">
  158. @Html.EditorFor(model => model.Description)
  159. @Html.ValidationMessageFor(model => model.Description)
  160. </div>
  161. <div class="editor-label">
  162. @Html.LabelFor(model => model.Image)
  163. </div>
  164. <div class="editor-field">
  165. <input type="file" name="imageFile" id="imageFile" />
  166. @Html.ValidationMessageFor(model => model.Image)
  167. </div>
  168. <p>
  169. @if (ViewBag.IsUpdate !=null && ViewBag.IsUpdate)
  170. {
  171. @Html.HiddenFor(model => model.ActorID);
  172. <input name="cmd" type="submit" value="Update" />
  173. }
  174. else
  175. {
  176. <input name="cmd" type="submit" value="Create" />
  177. }
  178. <input type="button" value="Cancel" id="btncancel" />
  179. </p>
  180. </fieldset>
  181. }
  182.  
  183. @model IEnumerable<AjaxFormMVC.Models.Actor>
  184. <p>
  185. @Html.ActionLink("Create New", "Create", null, new { @class = "createActor", title = "Create Actor" })
  186. </p>
  187. <table style="border: 1px solid black;" cellpadding="10">
  188. <tr>
  189. <th>
  190. @Html.DisplayNameFor(model => model.Name)
  191. </th>
  192. <th>
  193. @Html.DisplayNameFor(model => model.Description)
  194. </th>
  195. <th>
  196. @Html.DisplayNameFor(model => model.Image)
  197. </th>
  198. <th>
  199. </th>
  200. </tr>
  201. @foreach (var item in Model)
  202. {
  203. <tr>
  204. <td valign="top">
  205. @Html.DisplayFor(modelItem => item.Name)
  206. </td>
  207. <td valign="top">
  208. @Html.DisplayFor(modelItem => item.Description)
  209. </td>
  210. <td valign="top">
  211. <img src="@Url.Content("~/Content/Actors/" + item.ActorID + "/" + item.Image + "")" alt="@item.Name" width="100px" height="100px" />
  212. </td>
  213. <td valign="top">
  214. @Html.ActionLink("Edit", "Edit", new { id = item.ActorID }, new { @class = "createActor", title = "Edit Actor" })
  215. |
  216. @Html.ActionLink("Delete", "Delete", new { id = item.ActorID })
  217. </td>
  218. </tr>
  219. }
  220. </table>
  221.  
  222. @{
  223. ViewBag.Title = "Home Page";
  224. }
  225. @model IEnumerable<AjaxFormMVC.Models.Actor>
  226. @{
  227. Layout = null;
  228. }
  229. <!DOCTYPE html>
  230. <html>
  231. <head>
  232. <meta name="viewport" content="width=device-width" />
  233. <title>Index</title>
  234. <link href="@Url.Content("~/Content/themes/base/minified/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
  235. <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
  236. <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
  237. <script src="@Url.Content("~/Scripts/form_ajax.js")" type="text/javascript"></script>
  238. <script type="text/javascript">
  239. $(document).ready(function () {
  240.  
  241. $.ajaxSetup({ cache: false });
  242.  
  243. $(".createActor").live("click", function (e) {
  244. var $url = $(this).attr('href');
  245. var $title = $(this).attr('title');
  246.  
  247. var $dialog = $('<div id="modalDialog"></div>');
  248. $dialog.empty();
  249. $dialog.load($url).dialog({
  250. autoOpen: false,
  251. title: $title,
  252. resizable: false,
  253. height: 300,
  254. width: 380,
  255. show: { effect: 'drop', direction: "up" },
  256. modal: true,
  257. draggable: true
  258. });
  259. $dialog.dialog('open');
  260. return false;
  261. });
  262.  
  263. $("#btncancel").live("click", function (e) {
  264. $("#modalDialog").dialog('close');
  265. });
  266. });
  267. </script>
  268. </head>
  269. <body>
  270. <div id="actorList">
  271. @Html.Partial("_ActorList", Model)
  272. </div>
  273. </body>
  274. </html>
  275.  
  276. public class Student
  277. {
  278. public int id { get; set; }
  279. [DataType(DataType.ImageUrl)]
  280. public string Image { get; set; }
  281. }
  282. public class StudentrDbContext : DbContext
  283. {
  284. public DbSet<Student> Student { get; set; }
  285. }
  286.  
  287. public string ImageContentType {get;set;}
  288. public byte[] Image {get;set;}
  289.  
  290. public class Student
  291. {
  292. public int id { get; set; }
  293. public string ImageContentType {get;set;}
  294. public byte[] Image {get;set;}
  295. }
  296.  
  297. [HttpPost]
  298. public ActionResult Create(ApplicationViewModel vm, HttpPostedFileBase Image)
  299. {
  300. if (ModelState.IsValid)
  301. {
  302. if (Image != null)
  303. {
  304. vm.Application.ImageMimeType = Image.ContentType;
  305. vm.Application.Image = new byte[Image.ContentLength];
  306. Image.InputStream.Read(vm.Application.Image, 0, Image.ContentLength);
  307. }
  308.  
  309. try
  310. {
  311. // TODO: Add save logic here
  312. _provider.SaveApplication(vm);
  313.  
  314. TempData["success"] = true;
  315. TempData["message"] = String.Format("Sucessfully added {0}", vm.Application.ApplicationName);
  316. }
  317. catch (Exception)
  318. {
  319. TempData["error"] = true;
  320. TempData["message"] = String.Format("Unsucessfully Added {0}", vm.Application.ApplicationName);
  321. }
  322.  
  323. return RedirectToAction("Index");
  324. }
  325.  
  326. vm = _provider.ReInitialiseVm(vm);
  327.  
  328. return View("Shape", vm);
  329. }
  330.  
  331. vm.Application.ImageMimeType = Image.ContentType;
  332. vm.Application.Image = new byte[Image.ContentLength];
  333. Image.InputStream.Read(vm.Application.Image, 0, Image.ContentLength);
  334.  
  335. <td>
  336. @if (item.Image == null)
  337. {
  338. @:None
  339. } else {
  340. <img width="48" src="data:@item.ImageMimeType;base64,@(Convert.ToBase64String(item.Image))" />
  341. }
  342. </td>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement