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 LinqInclude.Models;
using LinqInclude.ViewModels;
namespace LinqInclude.Controllers
{
public class BooksController : Controller
{
private LinqIncludeFunctionEntities1 db = new LinqIncludeFunctionEntities1();
// GET: /Books/
public ActionResult Index()
{
var city = "Hyderabad";
var books = db.Books.Include(b => b.Library.City).FirstOrDefault();
var result = db.Books.Include(x => x.Library.City.State).ToList();
var result2 = result.Select(x => new AllBooks { StateName = x.Library.City.State.StateName,BookName = x.BookName, LibraryName= x.Library.LibraryName,CityName=x.Library.City.CityName});
return View(result2.ToList());
}
// GET: /Books/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Book book = db.Books.Find(id);
if (book == null)
{
return HttpNotFound();
}
return View(book);
}
// GET: /Books/Create
public ActionResult Create()
{
ViewBag.LibrariesId = new SelectList(db.Libraries, "Id", "LibraryName");
return View();
}
// POST: /Books/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="Id,BookName,LibrariesId")] Book book)
{
if (ModelState.IsValid)
{
db.Books.Add(book);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.LibrariesId = new SelectList(db.Libraries, "Id", "LibraryName", book.LibrariesId);
return View(book);
}
// GET: /Books/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Book book = db.Books.Find(id);
if (book == null)
{
return HttpNotFound();
}
ViewBag.LibrariesId = new SelectList(db.Libraries, "Id", "LibraryName", book.LibrariesId);
return View(book);
}
// POST: /Books/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="Id,BookName,LibrariesId")] Book book)
{
if (ModelState.IsValid)
{
db.Entry(book).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.LibrariesId = new SelectList(db.Libraries, "Id", "LibraryName", book.LibrariesId);
return View(book);
}
// GET: /Books/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Book book = db.Books.Find(id);
if (book == null)
{
return HttpNotFound();
}
return View(book);
}
// POST: /Books/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Book book = db.Books.Find(id);
db.Books.Remove(book);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
View:
@model IEnumerable
@{
ViewBag.Title = "Index";
}
Index
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.BookName)
@Html.DisplayNameFor(model => model.StateName)
@Html.DisplayNameFor(model => model.LibraryName)
@Html.DisplayNameFor(model => model.CityName)
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.BookName)
@Html.DisplayFor(modelItem => item.StateName)
@Html.DisplayFor(modelItem => item.LibraryName)
@Html.DisplayFor(modelItem => item.CityName)
}
Delete:
@model LinqInclude.Models.Book
@{
ViewBag.Title = "Delete";
}
Delete
Are you sure you want to delete this?
Book
-
@Html.DisplayNameFor(model => model.BookName)
-
@Html.DisplayFor(model => model.BookName)
-
@Html.DisplayNameFor(model => model.Library.LibraryName)
-
@Html.DisplayFor(model => model.Library.LibraryName)
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
|
@Html.ActionLink("Back to List", "Index")
}
State Index..cshtml
@model IEnumerable
@{
ViewBag.Title = "Index";
}
Index
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.StateName)
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.StateName)
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
}
Partial Views in MVC
Include Function
Using Cookies
Update Multiple Items
Edit,Update Multiple Items
Uploading Files in MVC