Merhaba arkadaşlar bugun sizlere asp.net mvc nin controoller ve view ksımınaan bahsedeceğim. Geçen yazımda model kısmına detaylı bir şekilde anlatmıştım

Model kısmını yazdıktan sonra controller i yazıyoruz geçenki projeye devam ediyoruz

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OrganizationDetails.Models;

namespace OrganizationDetails.Controllers
{
public class EnrollmentController : Controller
{
private OrganizationEmployeeEntities db = new OrganizationEmployeeEntities();

// GET: /Enrollment/
public ActionResult Index()
{
var enrollments = db.Enrollments.Include(e => e.Department).Include(e => e.Employee);
return View(enrollments.ToList());
}

// GET: /Enrollment/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Enrollment enrollment = db.Enrollments.Find(id);
if (enrollment == null)
{
return HttpNotFound();
}
return View(enrollment);
}

// GET: /Enrollment/Create
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, “DepartmentID”, “Title”);
ViewBag.EmployeeID = new SelectList(db.Employees, “EmployeeID”, “LastName”);
return View();
}

// POST: /Enrollment/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include=”EnrollmentID,Band,DepartmentID,EmployeeID”)] Enrollment enrollment)
{
if (ModelState.IsValid)
{
db.Enrollments.Add(enrollment);
db.SaveChanges();
return RedirectToAction(“Index”);
}

ViewBag.DepartmentID = new SelectList(db.Departments, “DepartmentID”, “Title”, enrollment.DepartmentID);
ViewBag.EmployeeID = new SelectList(db.Employees, “EmployeeID”, “LastName”, enrollment.EmployeeID);
return View(enrollment);
}

// GET: /Enrollment/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Enrollment enrollment = db.Enrollments.Find(id);
if (enrollment == null)
{
return HttpNotFound();
}
ViewBag.DepartmentID = new SelectList(db.Departments, “DepartmentID”, “Title”, enrollment.DepartmentID);
ViewBag.EmployeeID = new SelectList(db.Employees, “EmployeeID”, “LastName”, enrollment.EmployeeID);
return View(enrollment);
}

// POST: /Enrollment/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include=”EnrollmentID,Band,DepartmentID,EmployeeID”)] Enrollment enrollment)
{
if (ModelState.IsValid)
{
db.Entry(enrollment).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction(“Index”);
}
ViewBag.DepartmentID = new SelectList(db.Departments, “DepartmentID”, “Title”, enrollment.DepartmentID);
ViewBag.EmployeeID = new SelectList(db.Employees, “EmployeeID”, “LastName”, enrollment.EmployeeID);
return View(enrollment);
}

// GET: /Enrollment/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Enrollment enrollment = db.Enrollments.Find(id);
if (enrollment == null)
{
return HttpNotFound();
}
return View(enrollment);
}

// POST: /Enrollment/Delete/5
[HttpPost, ActionName(“Delete”)]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Enrollment enrollment = db.Enrollments.Find(id);
db.Enrollments.Remove(enrollment);
db.SaveChanges();
return RedirectToAction(“Index”);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

Şimdi ise view kısımlarını yazıyoruz
————————————————-create kısmı——————————-
@model OrganizationDetails.Models.Enrollment
@{
    ViewBag.Title = “Create”;
}
<h2>Create</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class=”form-horizontal”>
        <h4>Enrollment</h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class=”form-group”>
            @Html.LabelFor(model => model.Band, new { @class = “control-label col-md-2” })
            <div class=”col-md-10″>
                @Html.EditorFor(model => model.Band)
                @Html.ValidationMessageFor(model => model.Band)
            </div>
        </div>
        <div class=”form-group”>
            @Html.LabelFor(model => model.DepartmentID, “DepartmentID”, new { @class = “control-label col-md-2” })
            <div class=”col-md-10″>
                @Html.DropDownList(“DepartmentID”, String.Empty)
                @Html.ValidationMessageFor(model => model.DepartmentID)
            </div>
        </div>
        <div class=”form-group”>
            @Html.LabelFor(model => model.EmployeeID, “EmployeeID”, new { @class = “control-label col-md-2” })
            <div class=”col-md-10″>
                @Html.DropDownList(“EmployeeID”, String.Empty)
                @Html.ValidationMessageFor(model => model.EmployeeID)
            </div>
        </div>
        <div class=”form-group”>
            <div class=”col-md-offset-2 col-md-10″>
                <input type=”submit” value=”Create” class=”btn btn-default” />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink(“Back to List”, “Index”)
</div>
@section Scripts {
    @Scripts.Render(“~/bundles/jqueryval”)
}
——————————————-Devete kısmı———————————-
@model OrganizationDetails.Models.Enrollment
@{
    ViewBag.Title = “Delete”;
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Enrollment</h4>
    <hr />
    <dl class=”dl-horizontal”>
        <dt>
            @Html.DisplayNameFor(model => model.Band)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Band)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Department.Title)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Department.Title)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Employee.LastName)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Employee.LastName)
        </dd>
    </dl>
    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        <div class=”form-actions no-color”>
            <input type=”submit” value=”Delete” class=”btn btn-default” /> |
            @Html.ActionLink(“Back to List”, “Index”)
        </div>
    }
</div>
ü
,
iyi çalışmalar
Ramazan CESUR