Advertisement
Guest User

Midterm2

a guest
May 30th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1. using Midterm2.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7.  
  8. namespace Midterm2.Controllers
  9. {
  10.  
  11. public class PatientController : Controller
  12. {
  13. Context db = new Context();
  14. // GET: Patient
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19.  
  20. public ActionResult Login(string Username, string Password)
  21. {
  22. if (Username == "ibu" && Password == "admin")
  23. {
  24. return RedirectToAction("List");
  25. }
  26. else
  27. return View("Index");
  28. }
  29.  
  30. public ActionResult List()
  31. {
  32. List<Patient> p = db.Patients.Include("Doctor").ToList();
  33. ViewData["patients"] = p;
  34. return View();
  35. }
  36.  
  37. public ActionResult Create()
  38. {
  39. List<Patient> p = db.Patients.Include("Doctor").ToList();
  40. ViewData["patients"] = p;
  41. List<Doctor> d = db.Doctors.ToList();
  42. ViewData["doctors"] = d;
  43.  
  44. return View();
  45. }
  46.  
  47. public ActionResult SavePatient(string Firstname, string Lastname, string BirthPlace, DateTime DateOfBirth, int DoctorId)
  48. {
  49. //List<Patient> pa = db.Patients.Include("Doctor").ToList();
  50. //ViewData["patients"] = pa;
  51. bool Active = true;
  52. Patient p = new Patient
  53. {
  54. Firstname = Firstname,
  55. Lastname = Lastname,
  56. BirthPlace = BirthPlace,
  57. Active = Active,
  58. DateOfBirth = DateOfBirth,
  59. DoctorId = DoctorId
  60. };
  61. db.Patients.Add(p);
  62. db.SaveChanges();
  63. return RedirectToAction("List");
  64. }
  65.  
  66. public ActionResult Delete(int PatientId)
  67. {
  68. Patient p = db.Patients.Find(PatientId);
  69. db.Patients.Remove(p);
  70. db.SaveChanges();
  71. return RedirectToAction("List");
  72. }
  73.  
  74.  
  75.  
  76. public ActionResult Edit(int PatientId)
  77. {
  78.  
  79.  
  80. Patient p = db.Patients.Find(PatientId);
  81. ViewData["patient"] = p;
  82. List<Doctor> d = db.Doctors.ToList();
  83. ViewData["doctors"] = d;
  84. return View();
  85. }
  86.  
  87. public ActionResult UpdatePatient(int PatientId, bool Active, string Firstname, string Lastname, string BirthPlace, DateTime DateOfBirth, int DoctorId)
  88. {
  89. Patient p = db.Patients.Find(PatientId);
  90. if (p != null)
  91. {
  92. p.Firstname = Firstname;
  93. p.Lastname = Lastname;
  94. p.BirthPlace = BirthPlace;
  95. p.DateOfBirth = DateOfBirth;
  96. p.Active = Active;
  97. p.DoctorId = DoctorId;
  98.  
  99. }
  100. db.SaveChanges();
  101.  
  102. return RedirectToAction("List");
  103.  
  104. }
  105. }
  106. }
  107.  
  108. /////////////////////////////////////////////
  109.  
  110. using System;
  111. using System.Collections.Generic;
  112. using System.Linq;
  113. using System.Web;
  114.  
  115. namespace Midterm2.Models
  116. {
  117. public class Doctor
  118. {
  119. public int Id { get; set; }
  120. public string Firstname { get; set; }
  121. public string Lastname { get; set; }
  122. }
  123. }
  124. //////////////////////////////////////////
  125. using System;
  126. using System.Collections.Generic;
  127. using System.ComponentModel;
  128. using System.Linq;
  129. using System.Web;
  130.  
  131. namespace Midterm2.Models
  132. {
  133. public class Patient
  134. {
  135. public int Id { get; set; }
  136. public string Firstname { get; set; }
  137. public string Lastname { get; set; }
  138. public DateTime DateOfBirth { get; set; }
  139. [DefaultValue(true)]
  140. public bool Active { get; set; }
  141. public string BirthPlace { get; set; }
  142. public int DoctorId { get; set; }
  143. public Doctor Doctor { get; set; }
  144.  
  145. public Patient()
  146. {
  147. Active = true;
  148. }
  149.  
  150.  
  151.  
  152. }
  153. }
  154.  
  155. //////////////////////////////////////////////////////
  156.  
  157. using Midterm2.Models;
  158. using System;
  159. using System.Collections.Generic;
  160. using System.Data.Entity;
  161. using System.Linq;
  162. using System.Web;
  163.  
  164. namespace Midterm2
  165. {
  166. public class Context : DbContext
  167. {
  168. public Context() : base("Hospital")
  169. {
  170.  
  171. }
  172.  
  173. public DbSet<Patient> Patients { get; set; }
  174.  
  175. public DbSet<Doctor> Doctors { get; set; }
  176. }
  177. }
  178. ///////////////////////////////////////////////
  179.  
  180. @using Midterm2.Models
  181. @{
  182. ViewBag.Title = "Create";
  183. List<Doctor> d = ViewData["doctors"] as List<Doctor>;
  184. }
  185.  
  186. <h2>Add Patient</h2>
  187.  
  188. <form action="SavePatient" method="post">
  189. <table>
  190. <tr>
  191. <td>Firstname</td>
  192. <td><input type="text" name="Firstname" class="form-control" /></td>
  193. </tr>
  194. <tr>
  195. <td>Lastname</td>
  196. <td><input type="text" name="Lastname" class="form-control" /></td>
  197. </tr>
  198. <tr>
  199. <td>Date of Birth</td>
  200. <td><input type="date" name="DateOfBirth" class="form-control" /></td>
  201. </tr>
  202. <tr>
  203. <td>Birth place</td>
  204. <td><input type="text" name="BirthPlace" class="form-control" /></td>
  205.  
  206. </tr>
  207. <tr>
  208. <td>Doctor</td>
  209. <td>
  210. <select name="DoctorId" class="form-control">
  211. @foreach (var item in d)
  212. {
  213. <option value=@item.Id>@item.Firstname @item.Lastname</option>
  214. }
  215. </select>
  216. </td>
  217.  
  218. </tr>
  219. <tr>
  220. <td><input type="submit" name="Save" class="btn btn-success" /></td>
  221. </tr>
  222. </table>
  223. </form>
  224.  
  225. ///////////////////////////////////////////
  226.  
  227.  
  228. @using Midterm2.Models
  229. @using Midterm2.Controllers
  230.  
  231. @model Midterm2.Models.Patient
  232. @{
  233. ViewBag.Title = "EditPatient";
  234. Patient p = ViewData["patient"] as Patient;
  235. List<Doctor> d = ViewData["doctors"] as List<Doctor>;
  236. // List<b> active = ViewData["active"] as List<b>;
  237.  
  238.  
  239.  
  240. bool ValueT = true;
  241. bool ValueF = false;
  242.  
  243. }
  244.  
  245. <h2>Edit Patient : @p.Firstname @p.Lastname</h2>
  246.  
  247. <form action="UpdatePatient" method="post">
  248. <input type="hidden" name="PatientId" value="@p.Id" />
  249. <table>
  250. <tr>
  251. <td>Firstname</td>
  252. <td><input type="text" name="Firstname" class="form-control" /></td>
  253. </tr>
  254. <tr>
  255. <td>Lastname</td>
  256. <td><input type="text" name="Lastname" class="form-control" /></td>
  257. </tr>
  258. <tr>
  259. <td>Date of Birth</td>
  260. <td><input type="date" name="DateOfBirth" class="form-control" /></td>
  261. </tr>
  262. <tr>
  263. <td>Birth place</td>
  264. <td><input type="text" name="BirthPlace" class="form-control" /></td>
  265.  
  266. </tr>
  267.  
  268. <tr>
  269. <td>Doctor</td>
  270. <td>
  271. <select name="DoctorId" class="form-control">
  272. @foreach (var item in d)
  273. {
  274. <option value=@item.Id>@item.Firstname @item.Lastname</option>
  275. }
  276. </select>
  277. </td>
  278.  
  279. </tr>
  280. <tr>
  281. <td>Active</td>
  282. <td>
  283. <select name="Active" class="form-control">
  284.  
  285. <option value = @ValueT>Active</option>
  286. <option value = @ValueF>Not-Active</option>
  287.  
  288. </select>
  289. </td>
  290.  
  291. </tr>
  292.  
  293. <tr>
  294. <td><input type="submit" name="Save Changes" class="btn btn-success" /></td>
  295. </tr>
  296. </table>
  297. </form>
  298. //////////////////////////////////////////////////////////
  299.  
  300.  
  301. @{
  302. ViewBag.Title = "Index";
  303. }
  304. <div class="container">
  305. <div class="row">
  306. <div class="col-md-2 col-md-offset-2">
  307. <img src="https://cdn4.iconfinder.com/data/icons/general27/png/128/administrator.png" alt="Alternate Text" />
  308. </div>
  309.  
  310. <div class="col-md-3">
  311. <h4 class="text-center">Welcome to</h4>
  312. <h1 class="text-left">IBU Hospital</h1>
  313. <br />
  314. </div>
  315.  
  316. @*<div class="col-md-3">
  317. <h1>IBU Hospital</h1>
  318. </div>*@
  319.  
  320. </div>
  321.  
  322. </div>
  323.  
  324. <form action="Login" method="post">
  325. <table>
  326. <tr class="text-right">
  327. <td class="col-md-4">Username</td>
  328. <td class="col-md-4"><input type="text" name="Username" class="form-control" /></td>
  329. </tr>
  330.  
  331. <tr class="text-right">
  332. <td class="col-md-4">Password</td>
  333. <td class="col-md-4"><input type="password" name="Password" class="form-control" /></td>
  334. </tr>
  335.  
  336. <tr>
  337. <td class="col-md-4"></td>
  338. <td class="col-md-4 col-md-offset-4">
  339. <input type="submit" name="Submit" class="btn btn-success" />
  340. </td>
  341.  
  342. </tr>
  343. </table>
  344. </form>
  345.  
  346. ////////////////////////////////////////////////////////
  347.  
  348. @using Midterm2.Models
  349. @{
  350. ViewBag.Title = "List";
  351. List<Patient> p = ViewData["patients"] as List<Patient>;
  352. }
  353.  
  354. <h2>Patient List</h2>
  355. <br />
  356. <a href="/Patient/Create" class="btn btn-success" name="New Patient">New Patient </a>
  357. <br />
  358. <form>
  359. <table class="table">
  360. <thead>
  361.  
  362. <tr>
  363. <td>Firstname</td>
  364. <td>Lastname</td>
  365. <td>Active</td>
  366. <td>Birth Place</td>
  367. <td>Date of Birth</td>
  368. <td>Doctor</td>
  369. <td>Actions</td>
  370. </tr>
  371. </thead>
  372.  
  373. <tbody>
  374. @foreach (var item in p)
  375. {
  376. <tr>
  377. <td>@item.Firstname</td>
  378. <td>@item.Lastname</td>
  379. <td>@item.Active</td>
  380. <td>@item.BirthPlace</td>
  381. <td>@item.DateOfBirth</td>
  382. <td>@item.Doctor.Firstname @item.Doctor.Lastname</td>
  383. <td>
  384.  
  385.  
  386. <a href="~/Patient/Edit?PatientId=@item.Id" name="Edit">Edit</a> |
  387. <a href="~/Patient/Delete?PatientId=@item.Id" name="Delete">Delete</a>
  388.  
  389. </td>
  390.  
  391.  
  392.  
  393. </tr>
  394. }
  395. </tbody>
  396.  
  397. <tr>
  398. <td><a href="~/Patient/Index" class="btn btn-success" name="Logout">Logout</a></td>
  399. </tr>
  400. </table>
  401. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement