You are on page 1of 10

public class CategoryController : Controller

{
private readonly ApplicationDbContext _db;

public CategoryController(ApplicationDbContext db)


{
_db = db;
}
public ActionResult Index()
{
return View(_db.Category.ToList());
}

public ActionResult Details(int? id)


{
Category newCrud = _db.Category.Find(id);
return View(newCrud);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Category newCrud)
{
if (ModelState.IsValid)
{
_db.Category.Add(newCrud);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(newCrud);
}

public ActionResult Edit(int? id)


{
var newCrud = _db.Category.Where(x => x.Id == id).FirstOrDefault();
if (newCrud != null)
{
TempData["ID"] = id;
TempData.Keep();
return View(newCrud);
}
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Category newc)
{
var id = (int)TempData["id"];
var newCrud = _db.Category.Where(x=>x.Id == id).FirstOrDefault();
if (newCrud != null)
{
newCrud.Name = newc.Name;
_db.Entry(newCrud).State = EntityState.Modified;
_db.SaveChanges();
}
return RedirectToAction("Index");
}

public ActionResult Delete(int? id)


{
Category newCrud = _db.Category.Find(id);
return View(newCrud);
}

[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Category newCrud = _db.Category.Find(id);
_db.Category.Remove(newCrud);

_db.SaveChanges();
return RedirectToAction("Index");
}
}

public class SubCategoryController : Controller


{
private readonly ApplicationDbContext _db;

public SubCategoryController(ApplicationDbContext db)


{
_db = db;
}
public ActionResult Index()
{
var cat = _db.SubCategory.ToList();
foreach (var item in cat)
{
item.category = _db.Category.Where(t => t.Id ==
item.CategoryId).FirstOrDefault();
}
return View(cat);
}

public ActionResult Create()


{

CategoryAndSubCategoryModel cat = new CategoryAndSubCategoryModel();


cat.categoryList = _db.Category.ToList();
cat.subCategoryList = _db.SubCategory.Select(t => t.Name).ToList();

//List<string> scList = new List<string>();


//foreach (var item in _db.SubCategory)
//{
// scList.Add(item.Name);

//}
//cat.subCategoryList = scList;
return View(cat);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CategoryAndSubCategoryModel newCrud)
{
if (ModelState.IsValid)
{

var subCategory = new SubCategory()


{
CategoryId = newCrud.subCategory.CategoryId,
Name = newCrud.subCategory.Name
};

_db.SubCategory.Add(subCategory);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(newCrud);
}

public ActionResult Edit(int? id)


{
CategoryAndSubCategoryModel cat = new CategoryAndSubCategoryModel();

cat.categoryList = _db.Category.ToList();

cat.subCategoryList = _db.SubCategory.Select(t => t.Name).ToList();

cat.subCategory = _db.SubCategory.Find(id);
if (cat.subCategory != null)
{
TempData["ID"] = id;
TempData.Keep();
return View(cat);
}
return View();

}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(CategoryAndSubCategoryModel newc)
{
var id = (int)TempData["ID"];
var newcrud = _db.SubCategory.Where(x => x.Id == id).FirstOrDefault();
if (newc != null)
{
newcrud.Name = newc.subCategory.Name;
newcrud.CategoryId = newc.subCategory.CategoryId;
_db.Entry(newcrud).State = EntityState.Modified;
_db.SaveChanges();
}
return RedirectToAction("Index");
}

public ActionResult Delete(int? id)


{
SubCategory newCrud = _db.SubCategory.Find(id);
return View(newCrud);
}

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
SubCategory newCrud = _db.SubCategory.Find(id);
_db.SubCategory.Remove(newCrud);

_db.SaveChanges();
return RedirectToAction("Index");
}
}

@model Category
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {

@Html.AntiForgeryToken()
<div class="form-horozontal">
<h4>Category</h4>
<hr />
<div class="form-group">

@Html.LabelFor(model => model.Name, htmlAttributes: new { @class =


"control-lable col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class =
"text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
<input type="button" value="Back to List" class="btn btn-success"
onclick="history.back()" />
</div>
</div>
</div>

@model Category
@{
ViewData["Title"] = "Delete";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Delete Category</h2>
<div>
<hr />
<dl>
<dt>
@Html.DisplayNameFor(model=>model.Name)
</dt>
<dd>
@Html.DisplayFor(model=>model.Name)
</dd>
</dl>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-danger" />
<input type="button" value="Back to List" class="btn btn-success"
onclick="history.back()" />
</div>
}
</div>

@model Category
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<h2 class="text-info">Edit Category</h2>
<br />

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)

<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class =
"comtrol-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class =
"text-danger" })
</div>
<div class="form-group">
<div class="col-md-offset col-md-10">
<input type="submit" value="Update" class="btn btn-primary" />
<input type="button" value="Back to List" class="btn btn-
success" onclick="history.back()"/>
</div>
</div>
</div>
</div>
}

@model IEnumerable<Category>
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Category</h2>
<p>
<div style="text-align:right">
<a asp-action="Create" class="btn btn-info">Create New</a>
</div>
</p>
<table class="table table-hover table-responsive">
<tr>
<th>
@Html.DisplayNameFor(model=>model.Name)
</th>
<th></th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.Name)
</td>
<td>
<div style="text-align:right">
@Html.ActionLink("Edit", "Edit", new { id = item.Id },
new { @class = "btn btn-primary" })
@Html.ActionLink("Delete", "Delete", new { id = item.Id
}, new { @class = "btn btn-danger" })

</div>
</td>
</tr>
}
</table>

@model Sample.Models.ViewModel.CategoryAndSubCategoryModel
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm()) {

@Html.AntiForgeryToken()
<div class="form-horozontal">
<h4>Create SubCategory</h4>
<hr />
<div class="row">
<div class="col-md-7">
<div class="form-group">
@Html.LabelFor(model => model.subCategory.CategoryId, htmlAttributes:
new { @class = "control-lable col-md-2" })
<div class="col-md-10">

@Html.DropDownListFor(model=>model.subCategory.CategoryId,Model.categoryList.Select
(sc=>new SelectListItem()
{
Text=sc.Name,
Value=sc.Id.ToString()
}),new { @class = "form-control" ,id="ddCategoryList"})

</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.subCategory.Name, htmlAttributes: new
{ @class = "control-lable col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.subCategory.Name, new
{ htmlAttributes = new { @class = "form-control",id="subCategory_Name"} })
@Html.ValidationMessageFor(model => model.subCategory.Name, "", new
{ @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
<input type="button" value="Back to List" class="btn btn-success"
onclick="history.back()" />
</div>
</div>
</div>
<div class="col-md-5">
<p>Existingsub</p>
<table class="table table-bordered">

@foreach (var item in Model.subCategoryList)


{
<tr>
<td>
@item.ToString()
</td>

</tr>
}
</table>
</div>
</div>
</div>

@model SubCategory
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Delete Category</h2>
<div>
<hr />
<dl>
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
<span id="Name">@Html.DisplayFor(model => model.Name)</span>
</dd>
</dl>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-danger" />
<input type="button" value="Back to List" class="btn btn-success"
onclick="history.back()" />
</div>
}
</div>

@model Sample.Models.ViewModel.CategoryAndSubCategoryModel
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 class="text-info">Edit Category</h2>


<h2>Create</h2>
@using (Html.BeginForm())
{

@Html.AntiForgeryToken()
<div class="form-horozontal">
<h4>SubCategory</h4>
<hr />
<div class="row">
<div class="col-md-7">
<div class="form-group">
@Html.LabelFor(model => model.subCategory.CategoryId,
htmlAttributes: new { @class = "control-lable col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model =>
model.subCategory.CategoryId, Model.categoryList.Select(sc => new SelectListItem()
{
Text = sc.Name,
Value = sc.Id.ToString()
}), new { @class = "form-control" ,id="ddCategoryList"})

</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.subCategory.Name, htmlAttributes:
new { @class = "control-lable col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.subCategory.Name, new
{ htmlAttributes = new { @class = "form-control",id="subCategory_Name"} })
@Html.ValidationMessageFor(model => model.subCategory.Name,
"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-primary"
/>
<input type="button" value="Back to List" class="btn btn-
success" onclick="history.back()" />
</div>
</div>
</div>
<div class="col-md-5">
<p>Existingsub</p>
<table class="table table-hover table-responsive">

@foreach (var item in Model.subCategoryList)


{
<tr>
<td>
@item.ToString()
</td>

</tr>
}
</table>
</div>
</div>
</div>

@model IEnumerable<SubCategory>
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>SubCategory</h2>
<p>
<div style="text-align:right">
<a asp-action="Create" class="btn btn-info">Create New</a>
</div>
</p>
<table class="table table-hover table-responsive">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.category.Name)
</th>
<th></th>
</tr>
@foreach(var item in Model)
{
<tr>

<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.category.Name)
</td>
<td>
<div style="text-align:right">
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class =
"btn btn-primary" })
@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class
= "btn btn-danger" })

</div>
</td>
</tr>
}
</table>

You might also like