ASP.NET MVC 使用模板视图
根据上课讲解和个人课程项目的实践
创建一个MVC项目,我这里命名为了Text1025
1.Index页面
1.首先Model文件夹下创建Photo类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Text1025.Models
{
public class Photo
{
public string photographId { get; set; }
public Nullable<int> photographLevelId { get; set; }
public string photographName { get; set; }
public int LengthPixel { get; set; }
public int widthPixel { get; set; }
public string photographIntroduction { get; set; }
}
}
再在Model文件夹创建一个PhotoService类,用于保存增删改查需要用到的方法
2.添加一个名为PhotoController的控制器,选择包含读写操作的控制器模板
在PhotoService类中创建一个GetAll方法,用于取得在该方法中插入的全部数据
public List<Photo> GetAll()
{
List<Photo> list = new List<Photo>()
{
new Photo
{
photographId = "C219230030",
photographLevelId = 1,
photographName = "01.jpg",
LengthPixel = 1800,
widthPixel = 2400,
photographIntroduction = "北极光"
},
new Photo
{
photographId = "C219230031",
photographLevelId = 2,
photographName = "02.jpg",
LengthPixel = 1800,
widthPixel = 2400,
photographIntroduction = "猫头鹰"
} ,
new Photo
{
photographId = "C219230032",
photographLevelId = 3,
photographName = "03.jpg",
LengthPixel = 1900,
widthPixel = 2380,
photographIntroduction = "大白兔"
}
};
return list;
}
转到Photo控制器中的index方法
PhotoService service = new PhotoService(); //创建一个PhotoService类的对象
// GET: Photo
public ActionResult Index()
{
return View(service.GetAll());
}
3.添加index方法的list模板视图,选择使用Photo类
点击添加,自动生成展示列表模板
在浏览器中查看,效果如下
2.Create页面
主页左上方有一个Create New链接,用于上传添加数据,但还未写操作方法,模板页仅为我们提供写代码的模板,但未实现具体方法,需要我们添加
在PhotoService类中添加一个Add方法
public List<Photo> Add(Photo photo)
{
List<Photo> list = GetAll();
list.Add(photo);
return list;
}
在控制器中的Create方法中添加实现的代码
// GET: Photo/Create
public ActionResult Create()
{
return View();
}
// POST: Photo/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
string photographId = collection["photographId"];
int photographLevelId = Convert.ToInt32(collection["photographLevelId"]);
string photographName = collection["photographName"];
int LengthPixel = Convert.ToInt32(collection["LengthPixel"]);
int widthPixel = Convert.ToInt32(collection["widthPixel"]);
string photographIntroduction = collection["photographIntroduction"];
Photo photo = new Photo
{
photographId= photographId,
photographLevelId= photographLevelId,
photographName= photographName,
LengthPixel= LengthPixel,
widthPixel= widthPixel,
photographIntroduction= photographIntroduction
};
// TODO: Add insert logic here
return View("Index",service.Add(photo));
}
catch
{
return View();
}
}
应注意return View("Index",service.Add(photo));这条语句,在模板中是return RedirectToAction(),表示返回执行相应的方法,可能会导致看不到插入的数据
右击Create方法名,添加模板视图
将会自动生成插入数据的模版页
3.Details页面
下面再来实现主页右边的Details方法,点击该方法可查看对应数据项的详细信息
首先在PhotoService类中添加一个Find方法,用于查找指定关键字对应的数据项的全部信息
public Photo Find(string id)
{
List<Photo> list = GetAll();
return list.Where(p => p.photographId == id).FirstOrDefault();
}
在控制器下的Details方法中添加代码
// GET: Photo/Details/5
public ActionResult Details(string id)
{
return View(service.Find(id));
}
为details方法添加details模板视图
4.Edit页面
接下来实现编辑操作,首先要获取选择到的对象信息,然后修改,修改的过程为先删除旧的对象,然后插入新修改的对象,实际项目中,ID不可更改,这里不做要求
PhotoService类中添加Edit方法
public List<Photo> Edit(Photo photo)
{
List<Photo> list = GetAll();
Photo photograph = list.Where(p => p.photographId == photo.photographId).FirstOrDefault();
list.Remove(photograph); //移除修改之前的对象
list.Add(photo); //添加修改之后的新对象
return list;
}
在控制器的Edit事件中添加相应代码,然后生成edit模型的视图
// GET: Photo/Edit/5
public ActionResult Edit(string id)
{
return View(service.Find(id));
}
// POST: Photo/Edit/5
[HttpPost]
public ActionResult Edit(string id, FormCollection collection)
{
try
{
string photographId = collection["photographId"];
int photographLevelId = Convert.ToInt32(collection["photographLevelId"]);
string photographName = collection["photographName"];
int LengthPixel = Convert.ToInt32(collection["LengthPixel"]);
int widthPixel = Convert.ToInt32(collection["widthPixel"]);
string photographIntroduction = collection["photographIntroduction"];
Photo photo = new Photo
{
photographId = photographId,
photographLevelId = photographLevelId,
photographName = photographName,
LengthPixel = LengthPixel,
widthPixel = widthPixel,
photographIntroduction = photographIntroduction
};
List<Photo> list = service.Edit(photo);
return View("Index",list);
}
catch
{
return View();
}
}
在浏览器中查看操作效果,修改后的数据会显示在最下列
5.Delete
photoService类中新建delete方法
public List<Photo> Delete(string id)
{
List<Photo> list = GetAll();
Photo photo = list.Where(p => p.photographId == id).FirstOrDefault();
list.Remove(photo);
return list;
}
控制器下
public List<Photo> Delete(string id)
{
List<Photo> list = GetAll();
Photo photo = list.Where(p => p.photographId == id).FirstOrDefault();
list.Remove(photo);
return list;
}
新建delete模型视图
在浏览器中运行可删除选中的对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Text1025.Models;
namespace Text1025.Models
{
public class PhotoService
{
//Photo photo = new Photo();
public List<Photo> GetAll()
{
List<Photo> list = new List<Photo>()
{
new Photo
{
photographId = "C219230030",
photographLevelId = 1,
photographName = "01.jpg",
LengthPixel = 1800,
widthPixel = 2400,
photographIntroduction = "北极光"
},
new Photo
{
photographId = "C219230031",
photographLevelId = 2,
photographName = "02.jpg",
LengthPixel = 1800,
widthPixel = 2400,
photographIntroduction = "猫头鹰"
} ,
new Photo
{
photographId = "C219230032",
photographLevelId = 3,
photographName = "03.jpg",
LengthPixel = 1900,
widthPixel = 2380,
photographIntroduction = "大白兔"
}
};
return list;
}
public Photo Find(string id)
{
List<Photo> list = GetAll();
return list.Where(p => p.photographId == id).FirstOrDefault();
}
public List<Photo> Add(Photo photo)
{
List<Photo> list = GetAll();
list.Add(photo);
return list;
}
public List<Photo> Edit(Photo photo)
{
List<Photo> list = GetAll();
Photo photograph = list.Where(p => p.photographId == photo.photographId).FirstOrDefault();
list.Remove(photograph); //移除修改之前的对象
list.Add(photo); //添加修改之后的新对象
return list;
}
public List<Photo> Delete(string id)
{
List<Photo> list = GetAll();
Photo photo = list.Where(p => p.photographId == id).FirstOrDefault();
list.Remove(photo);
return list;
}
public List<Photo> Search(string id, string introduction)
{
List<Photo> list =GetAll();
if (id != null && id != "")
{
list = list.Where(p => p.photographId == id).ToList();
}
if (introduction != null && introduction != "")
{
list = list.Where(p => p.photographIntroduction== id).ToList();
}
return list;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Text1025.Models; //引用模型
namespace Text1025.Controllers
{
public class PhotoController : Controller
{
PhotoService service = new PhotoService(); //创建一个PhotoService类的对象
// GET: Photo
public ActionResult Index()
{
return View(service.GetAll());
}
// GET: Photo/Details/5
public ActionResult Details(string id)
{
return View(service.Find(id));
}
// GET: Photo/Create
public ActionResult Create()
{
return View();
}
// POST: Photo/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
string photographId = collection["photographId"];
int photographLevelId = Convert.ToInt32(collection["photographLevelId"]);
string photographName = collection["photographName"];
int LengthPixel = Convert.ToInt32(collection["LengthPixel"]);
int widthPixel = Convert.ToInt32(collection["widthPixel"]);
string photographIntroduction = collection["photographIntroduction"];
Photo photo = new Photo
{
photographId= photographId,
photographLevelId= photographLevelId,
photographName= photographName,
LengthPixel= LengthPixel,
widthPixel= widthPixel,
photographIntroduction= photographIntroduction
};
// TODO: Add insert logic here
return View("Index",service.Add(photo));
}
catch
{
return View();
}
}
// GET: Photo/Edit/5
public ActionResult Edit(string id)
{
return View(service.Find(id));
}
// POST: Photo/Edit/5
[HttpPost]
public ActionResult Edit(string id, FormCollection collection)
{
try
{
string photographId = collection["photographId"];
int photographLevelId = Convert.ToInt32(collection["photographLevelId"]);
string photographName = collection["photographName"];
int LengthPixel = Convert.ToInt32(collection["LengthPixel"]);
int widthPixel = Convert.ToInt32(collection["widthPixel"]);
string photographIntroduction = collection["photographIntroduction"];
Photo photo = new Photo
{
photographId = photographId,
photographLevelId = photographLevelId,
photographName = photographName,
LengthPixel = LengthPixel,
widthPixel = widthPixel,
photographIntroduction = photographIntroduction
};
List<Photo> list = service.Edit(photo);
return View("Index",list);
}
catch
{
return View();
}
}
// GET: Photo/Delete/5
public ActionResult Delete(string id)
{
return View(service.Find(id));
}
// POST: Photo/Delete/5
[HttpPost]
public ActionResult Delete(string id, FormCollection collection)
{
try
{
return View("Index",service.Delete(id));
}
catch
{
return View();
}
}
}
}