public class FactoryController : Controller
{
ProductInfosBll bll = new ProductInfosBll();
CityBll city = new CityBll();
static int index = 1;//索引页
static int size = 2;//每页显示的数据
static int tCount = 0;//总条数
static int tPage = 0;//总页数
string where = "1=1";
/// <summary>
/// 显示产品信息界面
/// </summary>
/// <returns></returns>
// GET: Factory
public ActionResult Index()
{
GetCity();
return View(ShowBinds());
}
/// <summary>
/// 搜索产品信息
/// </summary>
/// <param name="txtProductName"></param>
/// <param name="Gid"></param>
/// <param name="BeginTime"></param>
/// <param name="EndTime"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Index(string txtProductName,string Gid, DateTime? BeginTime, DateTime? EndTime)
{
if (!string.IsNullOrEmpty(txtProductName))
{
where += " and ProductName like '%" + txtProductName + "%'";
}
if (!string.IsNullOrEmpty(Gid))
{
where += " and PId='" + Gid + "'";
}
if (BeginTime!=null)
{
where += " and ProductDate >='" + BeginTime + "'";
}
if (EndTime!=null)
{
where += " and ProductDate <='" + EndTime+"'";
}
return PartialView("_PartialPage1", ShowBinds());
}
/// <summary>
/// 绑定原始数据代码
/// </summary>
/// <returns></returns>
private List<ProductInfos> ShowBinds()
{
string sqlTable = "ProductInfos inner join ProductMades on ProductInfos.PId=ProductMades.PId";
FenPage page = new FenPage();//实例化参数的类
page.tableName = sqlTable;//表名称
page.indexCol = "id";//标识列名
page.pageSize = size;//每页显示的数据自增的
page.pageIndex = index-1;//总条数
page.orderCol = "id";//排序根据id
page.where = where;//条件
page.columns = "*";//显示的列
DataModel list = bll.ShowBinds(JsonConvert.SerializeObject(page));//bll层的语句方法
tCount = list.tCount;//总条数
tPage = list.tPage;//总页数
ViewBag.tCount = tCount;
ViewBag.tPage = tPage;
ViewBag.index = index ;
return JsonConvert.DeserializeObject<List<ProductInfos>>(JsonConvert.SerializeObject(list.proinfos));
}
/// <summary>
/// 绑定下拉框
/// </summary>
public void GetCity()
{
List<ProductMades> list = city.GetCityType();
var linq = from s in list
select new SelectListItem
{
Value=s.PId.ToString(),
Text=s.PName
};
ViewBag.GetCity = linq.ToList();
}
/// <summary>
/// 首页
/// </summary>
/// <returns></returns>
public ActionResult First()
{
index = 1;
return PartialView("_PartialPage1", ShowBinds());
}
/// <summary>
/// 上一页
/// </summary>
/// <returns></returns>
public ActionResult Prove()
{
if (index>0)
{
index--;
}
return PartialView("_PartialPage1", ShowBinds());
}
/// <summary>
/// 下一页
/// </summary>
/// <returns></returns>
public ActionResult Next()
{
if (index<tPage)
{
index++;
}
return PartialView("_PartialPage1", ShowBinds());
}
/// <summary>
/// 尾页
/// </summary>
/// <returns></returns>
public ActionResult Last()
{
index = tPage ;
return PartialView("_PartialPage1", ShowBinds());
}
/// <summary>
/// 添加产品信息界面
/// </summary>
/// <returns></returns>
public ActionResult AddProductInfos()
{
GetCity();
return View();
}
/// <summary>
/// 添加产品信息
/// </summary>
/// <returns></returns>
[HttpPost]
public int AddProductInfos(ProductInfos model,HttpPostedFileBase ProductImage)
{
string path = Path.Combine(Request.MapPath("/Img/"),ProductImage.FileName);
ProductImage.SaveAs(path);
model.ProductImage = "/Img/" + ProductImage.FileName;
return bll.InsertInfos(model);
}
/// <summary>
/// 删除产品信息
/// </summary>
public ActionResult DeleteProductInfos(string id)
{
int i = bll.DeleteInfos(id);
if (i>0)
{
Response.Write("<script>alert('删除成功!');location.href='/Factory/Index'</script>");
}
else
{
Response.Write("<script>alert('删除失败!')</script>");
}
return PartialView("_PartialPage1", ShowBinds());
}
/// <summary>
/// 修改产品信息界面
/// </summary>
/// <returns></returns>
public ActionResult UpdateProductInfos(int id)
{
GetCity();
ProductInfos list = ShowBinds().Find(m => m.ID == id);
return View(list);
}
/// <summary>
/// 修改产品信息界面
/// </summary>
/// <returns></returns>
[HttpPost]
public int UpdateProductInfos(ProductInfos model,HttpPostedFileBase ProductImage)
{
string path = Path.Combine(Request.MapPath("/Img/"), ProductImage.FileName);
ProductImage.SaveAs(path);
model.ProductImage = "/Img/"+ProductImage.FileName;
return bll.UpdateInfos(model);
}
}