Admin Controller:-
using GsMentors.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using System.IO;
namespace GsMentors.Controllers
{
public class AdminController : Controller
{
//
// GET: /Admin/
private gs_dbEntities con = new gs_dbEntities();
public ActionResult Index()
{
if (Session["userid"] != null)
return View();
else
return RedirectToAction("Login", "Admin");
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel call)
{
try
{
if (ModelState.IsValid)
{
if (!string.IsNullOrEmpty(call.UserName.ToString()) && !(string.IsNullOrEmpty(call.Password.ToString())))
{
var i = from o in con.User_Master where o.U_Name.Equals(call.UserName) && o.U_Password.Equals(call.Password) select o;
if (i.ToList().Count() > 0)
{
Session["userid"] = call.UserName.ToString();
return RedirectToAction("Index", "Admin");
}
else
{
ViewBag.err = "Invalid ID or Password!";
return View();
}
}
return View();
}
else
return View();
}
catch (Exception)
{
return View();
}
}
public ActionResult Category()
{
var data = con.Category_Master.ToList();
return View(data);
}
public ActionResult Comments()
{
return View();
}
public ActionResult addCat()
{
return View();
}
[HttpPost]
public ActionResult addCat(Category_Master model)
{
if(ModelState.IsValid)
{
con.Category_Master.Add(model);
con.SaveChanges();
return RedirectToAction("Category");
}
return View();
}
public ActionResult EditCat(int id=0)
{
Category_Master data = con.Category_Master.Find(id);
if(data==null)
{
return HttpNotFound();
}
return View(data);
}
[HttpPost]
public ActionResult EditCat(Category_Master model)
{
if(ModelState.IsValid)
{
con.Entry(model).State = EntityState.Modified;
con.SaveChanges();
return RedirectToAction("Category");
}
return View(model);
}
public ActionResult DeleteCat(int id=0)
{
Category_Master data = con.Category_Master.Find(id);
if(data==null)
{
return HttpNotFound();
}
con.Category_Master.Remove(data);
con.SaveChanges();
return RedirectToAction("Category");
}
//Blogs goes here...
public ActionResult Post()
{
var data = con.Blog_Master.Include(x => x.User_Master).Include(x => x.Category_Master);
return View(data.ToList());
}
public ActionResult addPost()
{
ViewBag.User_ID = new SelectList(con.User_Master, "ID", "U_Name");
ViewBag.Cat_ID = new SelectList(con.Category_Master, "ID", "Name");
return View();
}
[HttpPost]
public ActionResult addPost(Blog_Master data,IEnumerable<HttpPostedFileBase> image)
{
if(ModelState.IsValid)
{
foreach (HttpPostedFileBase file in image)
{
string cat_id = null;
if (file != null)
{
int max = 0;
int len = file.FileName.Length;
int startlen = len - 4;
string strlen = file.FileName.Substring(startlen);
var val=con.Blog_Master.FirstOrDefault(x=>x.ID.Equals(1));
if (val== null)
{
max = 0;
}
else
{
max = con.Blog_Master.Max(s => s.ID);
}
max++;
cat_id = "Blog" + "_" + max + strlen;
var path = Path.Combine(Server.MapPath("~/Image"), cat_id);
file.SaveAs(path);
}
Blog_Master obj = new Blog_Master();
obj.Cat_ID = data.Cat_ID;
obj.User_id = data.User_id;
obj.Title = data.Title;
obj.Tags = data.Tags;
obj.Pic = cat_id;
obj.Descr = data.Descr;
obj.Date = DateTime.Now.ToString();
con.Blog_Master.Add(obj);
con.SaveChanges();
return RedirectToAction("Post");
}
}
ViewBag.User_ID = new SelectList(con.User_Master, "ID", "U_Name");
ViewBag.Cat_ID = new SelectList(con.Category_Master, "ID", "Name");
return View(data);
}
public ActionResult EditPost(int id=0)
{
Blog_Master data = con.Blog_Master.Find(id);
if (data == null)
{
return HttpNotFound();
}
ViewBag.User_ID = new SelectList(con.User_Master, "ID", "U_Name");
ViewBag.Cat_ID = new SelectList(con.Category_Master, "ID", "Name");
return View(data);
}
[HttpPost]
public ActionResult EditPost(Blog_Master data,IEnumerable<HttpPostedFileBase> image)
{
if (ModelState.IsValid)
{
foreach (HttpPostedFileBase file in image)
{
string cat_id = null;
if (file != null)
{
int len = file.FileName.Length;
int startlen = len - 4;
string strlen = file.FileName.Substring(startlen);
cat_id = "Blog" + "_" + data.ID + strlen;
var path = Path.Combine(Server.MapPath("~/Image"), cat_id);
file.SaveAs(path);
}
Blog_Master obj = new Blog_Master();
obj.ID = data.ID;
obj.Cat_ID = data.Cat_ID;
obj.User_id = data.User_id;
obj.Title = data.Title;
obj.Tags = data.Tags;
obj.Pic = cat_id;
obj.Descr = data.Descr;
obj.Date = DateTime.Now.ToString();
con.Entry(obj).State = EntityState.Modified;
con.SaveChanges();
return RedirectToAction("Post");
}
}
ViewBag.User_ID = new SelectList(con.User_Master, "ID", "U_Name");
ViewBag.Cat_ID = new SelectList(con.Category_Master, "ID", "Name");
return View(data);
}
public ActionResult DeletePost(int id=0)
{
Blog_Master data = con.Blog_Master.Find(id);
if (data == null)
{
return HttpNotFound();
}
con.Blog_Master.Remove(data);
con.SaveChanges();
return RedirectToAction("Post");
}
public ActionResult Logout()
{
Session.Abandon();
return RedirectToAction("Login", "Admin");
}
}
}
No comments:
Post a Comment