[Bind(Exclude = "DataDodania")] [Table("Kurs", Schema = "dbo")] public partial class Kurs { public Kurs() { this.KursUser = new HashSet<KursUser>(); } [Key] [ScaffoldColumn(false)] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [DisplayName("Osoba prywatna:")] public bool Typ { get; set; } [Required] [DisplayName("Z:")] public string Skad { get; set; } [Required] [DisplayName("Do:")] public string Dokad { get; set; } [DataType(DataType.Date)] [DisplayName("Kiedy:")] public System.DateTime DataWyjazdu { get; set; } [DataType(DataType.Date)] [DisplayName("Data dodania:")] [ScaffoldColumn(false)] public System.DateTime DataDodania { get; set; } public virtual ICollection<KursUser> KursUser { get; set; } }Aby dodać kontroler klikamy PPM na folder i z menu kontekstowego wybieramy opcję Add->Controller (Rys. 2.42). Otworzy się okno, w którym musimy podać nazwę kontrolera oraz szablon na podstawie którego ma być wygenerowany kontroler.
public class Kurs1Controller : Controller { private FullContext db = new FullContext(); // GET: /Kurs1/ public ActionResult Index() { return View(db.Kursy.ToList()); } // // GET: /Kurs1/Details/5 public ActionResult Details(int id = 0) { Kurs kurs = db.Kursy.Find(id); if (kurs == null) { return HttpNotFound(); } return View(kurs); } // // GET: /Kurs1/Create public ActionResult Create() { return View(); } // // POST: /Kurs1/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Kurs kurs) { if (ModelState.IsValid) { db.Kursy.Add(kurs); db.SaveChanges(); return RedirectToAction("Index"); } return View(kurs); } // // GET: /Kurs1/Edit/5 public ActionResult Edit(int id = 0) { Kurs kurs = db.Kursy.Find(id); if (kurs == null) { return HttpNotFound(); } return View(kurs); } // // POST: /Kurs1/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Kurs kurs) { if (ModelState.IsValid) { db.Entry(kurs).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(kurs); } // // GET: /Kurs1/Delete/5 public ActionResult Delete(int id = 0) { Kurs kurs = db.Kursy.Find(id); if (kurs == null) { return HttpNotFound(); } return View(kurs); } // // POST: /Kurs1/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Kurs kurs = db.Kursy.Find(id); db.Kursy.Remove(kurs); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } }
@model IEnumerable<CoreModel.Models.Kurs> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> @Html.DisplayNameFor(model => model.Typ) </th> <th> @Html.DisplayNameFor(model => model.Skad) </th> <th> @Html.DisplayNameFor(model => model.Dokad) </th> <th> @Html.DisplayNameFor(model => model.DataWyjazdu) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Typ) </td> <td> @Html.DisplayFor(modelItem => item.Skad) </td> <td> @Html.DisplayFor(modelItem => item.Dokad) </td> <td> @Html.DisplayFor(modelItem => item.DataWyjazdu) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) </td> </tr> } </table>