Build Deep high-quality website with Core Pro 5.1.8 and Theme 1.0.6
BULDING CODE MANAGER PAGE C# MVC ASP.NET
BULDING CODE MANAGER PAGE C# MVC ASP.NET
Code edit update page
using megastore.Areas.admin.Models;
using megastore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace megastore.Areas.admin.Controllers
{
public class PagesController : Controller
{
megastoreEntities db = new megastoreEntities();
public ActionResult Index()
{
var data = db.Pages.OrderByDescending(x => x.id).ToList();
return View(data);
}
[HttpPost]
[HttpGet]
public ActionResult Delete(Int32 id)
{
var data = db.Pages.FirstOrDefault(x => x.id.Equals(id));
if (data != null)
{
var listdata = db.Pages.Where(x => x.id.Equals(data.id));
foreach (var item in listdata)
{
db.Pages.Remove(item);
}
db.Pages.Remove(data);
db.SaveChanges();
}
return RedirectToAction("Index", "Pages", new { id = data.id });
}
public ActionResult Edit(Int32 id)
{
var _Pages = db.Pages.FirstOrDefault(x => x.id.Equals(id));
PagesEdit model = new PagesEdit();
if (_Pages != null)
{
model.id= _Pages.id;
model.NAME= _Pages.NAME;
model.NOIDUNG= _Pages.NOIDUNG;
model.TEMPLATE= _Pages.TEMPLATE;
model.META_TAG= _Pages.META_TAG;
model.META_DES= _Pages.META_DES;
model.META_TITLE= _Pages.META_TITLE;
model.DATE_POST= _Pages.DATE_POST;
model.DATE_VIEW= _Pages.DATE_VIEW;
model.USER_POST= _Pages.USER_POST;
model.STATUS= _Pages.STATUS;
model.LANG= _Pages.LANG;
model.ALIAS= _Pages.ALIAS;
model.KEYID= _Pages.KEYID;
}
return View(model);
}
[HttpPost]
public ActionResult Edit(PagesEdit model)
{
if (ModelState.IsValid)
{
var _Pages = db.Pages.FirstOrDefault(x => x.id.Equals(model.id));
if ( _Pages != null)
{
_Pages.id = model.id;
_Pages.NAME = model.NAME;
_Pages.NOIDUNG = model.NOIDUNG;
_Pages.TEMPLATE = model.TEMPLATE;
_Pages.META_TAG = model.META_TAG;
_Pages.META_DES = model.META_DES;
_Pages.META_TITLE = model.META_TITLE;
_Pages.DATE_POST = model.DATE_POST;
_Pages.DATE_VIEW = model.DATE_VIEW;
_Pages.USER_POST = model.USER_POST;
_Pages.STATUS = model.STATUS;
_Pages.LANG = model.LANG;
_Pages.ALIAS = model.ALIAS;
_Pages.KEYID = model.KEYID;
db.SaveChanges();
ModelState.AddModelError("", "Đã Sửa Thành Công!");
}
}
return View(model);
}
public ActionResult Add()
{
PagesAdd model = new PagesAdd();
return View(model);
}
[HttpPost]
public ActionResult Add(PagesAdd model)
{
if (ModelState.IsValid)
{
Pages _Pages = new Pages();
var data = db.Pages.FirstOrDefault(x => x.id.Equals(model.id));
if (data == null)
{
_Pages.id = model.id;
_Pages.NAME = model.NAME;
_Pages.NOIDUNG = model.NOIDUNG;
_Pages.TEMPLATE = model.TEMPLATE;
_Pages.META_TAG = model.META_TAG;
_Pages.META_DES = model.META_DES;
_Pages.META_TITLE = model.META_TITLE;
_Pages.DATE_POST = model.DATE_POST;
_Pages.DATE_VIEW = model.DATE_VIEW;
_Pages.USER_POST = model.USER_POST;
_Pages.STATUS = model.STATUS;
_Pages.LANG = model.LANG;
_Pages.ALIAS = model.ALIAS;
_Pages.KEYID = model.KEYID;
db.Pages.Add(_Pages);
db.SaveChanges();
ModelState.AddModelError("", "Add new a item sucsecs full");
}
else
{
ModelState.AddModelError("", "tên này đã tồn tại. Hãy chọn tên khác");
}
}
return View(model);
}
}
}
Inex.cshtml
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Pages Index
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="/admin/Pages">Pages</a></li>
<li class="active"> Pages Index</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<section class="content">
<div class="box box-success">
<div class="box-body">
<div class="table-responsive">
<div class="box-body">
<form action="/admin/Pages/delete" method="post" enctype="multipart/form-data" id="form-Pages">
<a href="javascript:history.back()">
<button type="button" class="btn btn-flat btn-warning"><i class="fa fa-mail-reply-all"></i> Trở về</button>
</a>
<button type="button" class="btn btn-flat btn-danger" onclick="confirm('Xóa/Gỡ bỏ sẽ không phục hồi lại được! Bạn có chắc chắn muốn thực hiện việc này?') ? $('#form-Pages').submit() : false;" name="delete"><i class="fa fa-times"></i> Xóa chọn</button>
<button onclick="location.href='/admin/Pages/add'" type="button" class="btn btn-flat btn-success"><i class="fa fa-plus"></i> Thêm mới</button>
<hr>
<script type="text/javascript">
function Delete(x) {
if (confirm("Đối tượng sẽ bị xóa.Bạn có muốn không ?")) {
$.ajax({
type: "POST",
url: '@Url.Action("Delete", "Pages")',
data: { id: x }
});
}
}
$('#maintable').DataTable();
</script>
<div class="box-body">
<div >
@Html.ActionLink("Add new", "Add")
</div >
<table class=" table table-striped table-bordered table-hover " id="maintable" >
<thead>
<tr>
<th><input id="cbselectall" type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /> </th>
<th>id</th>
<th>NOIDUNG</th>
<th>TEMPLATE</th>
<th>DATE_POST</th>
<th>DATE_VIEW</th>
<th>USER_POST</th>
<th>STATUS</th>
<th>LANG</th>
<th>KEYID</th>
<th>Chức năng</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr >
<td><input name="selected[]" value="@item.id" type="checkbox" /></td>
<td>@item.id</td>
<td>@item.NOIDUNG</td>
<td>@item.TEMPLATE</td>
<td>@item.DATE_POST</td>
<td>@item.DATE_VIEW</td>
<td>@item.USER_POST</td>
<td>@item.STATUS</td>
<td>@item.LANG</td>
<td>@item.KEYID</td>
<td class="center">
<a class="btn btn-warning " href="/admin/Pages/Edit/@item.id" style="padding-bottom:1px;padding-top:1px; padding-left:5px;"><i class="fa fa-paste"></i> Edit </a>
<a class="btn btn-danger" onclick="Delete('@item.id')" data-id="@item.id" style="padding-bottom:1px;padding-top:1px; padding-left:5px;"><i class="fa fa-trash-o"></i>Delete</a></td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th></th>
<th>id</th>
<th>NOIDUNG</th>
<th>TEMPLATE</th>
<th>DATE_POST</th>
<th>DATE_VIEW</th>
<th>USER_POST</th>
<th>STATUS</th>
<th>LANG</th>
<th>KEYID</th>
<th>Chức năng</th>
</tr>
</tfoot>
</table>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</div>
<!-- /.row -->
</section>
<!-- /.content -->
Add.cshtml
@model Pages.PagesAdd
<!-- Content Header -->
<section class="content-header">
<h1>
<a href="/admin/Pages/index">
<button type="button" class="btn btn-flat btn-success"><i class="fa fa-mail-reply-all"></i> Trở về</button>
</a>
Pages Add
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Pages</a></li>
<li class="active"> Pages Add</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
@if(ViewBag.alert!=null) {
<div class="alert [email protected] alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa [email protected]"></i> Thông báo!</h4>
@ViewBag.alert
</div>
}
<!-- column -->
<div class="box">
<div class="box-body">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-plus"></i> Pages Add</h3>
</div>
<div class="box-body">
@using (Html.BeginForm("Add", "Pages", FormMethod.Post,new { @class = "form-horizontal", @id = "form-Pages" }))
{
@Html.ValidationSummary(true)
{
<div class="form-group">
<label class="col-sm-2 control-label">id</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.id, new { placeholder="id", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.id)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">NAME</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.NAME, new { placeholder="NAME", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.NAME)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">NOIDUNG</label>
<div class="col-sm-10">
@Html.TextAreaFor(x => x.NOIDUNG, new { placeholder="NOIDUNG", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.NOIDUNG)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">TEMPLATE</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.TEMPLATE, new { placeholder="TEMPLATE", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.TEMPLATE)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">META_TAG</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.META_TAG, new { placeholder="META_TAG", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.META_TAG)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">META_DES</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.META_DES, new { placeholder="META_DES", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.META_DES)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">META_TITLE</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.META_TITLE, new { placeholder="META_TITLE", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.META_TITLE)</div>
</div>
<div class="form-group" >
<label class="col-sm-2 control-label">DATE_POST</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.DATE_POST, new { @placeholder="DATE_POST", @class="form-control" ,@type="date" })
@Html.ValidationMessageFor(model => model.DATE_POST)
</div>
</div>
<div class="form-group" >
<label class="col-sm-2 control-label">DATE_VIEW</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.DATE_VIEW, new { @placeholder="DATE_VIEW", @class="form-control" ,@type="date" })
@Html.ValidationMessageFor(model => model.DATE_VIEW)
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER_POST</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.USER_POST, new { placeholder="USER_POST", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.USER_POST)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">STATUS</label>
<div class="col-sm-10">
@Html.CheckBoxFor(x => x.STATUS, new { @style="position: absolute; opacity: 1;"}) <br/>
@Html.ValidationMessageFor(model => model.STATUS)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">LANG</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.LANG, new { placeholder="LANG", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.LANG)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">ALIAS</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.ALIAS, new { placeholder="ALIAS", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.ALIAS)</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">KEYID</label>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.KEYID, new { placeholder="KEYID", @class="form-control "})<br/>
@Html.ValidationMessageFor(model => model.KEYID)</div>
</div>
<div align='right'>
<div class='box-footer'>
<button type='reset' class='btn btn-primary btn-flat'><i class='fa fa-times'> </i>Reset</button>
<button name='emp' type='submit' class='btn btn-success btn-flat'><i class='fa fa-save'> </i> Save changes</button>
</div>
</div>
}
}
</div>
</div>
</div>
</div>
</section>
0 Nhận xét
Hãy trở thành người đầu tiên viết chia sẽ cảm nghĩ của mình bên dưới nhé!
Thêm Bình luận