如何在没有数据库的情况下在 MVC 中制作删除和编辑方法
How to make Delete and Edit methods in MVC without DB
我在不使用数据库的情况下构建一个简单的 MVC CRUD,只是在 Repository 模型中创建方法 class。
为了更容易理解,我有 2 个模型 classes。我有一些属性的 MyNote 和有属性列表的 NoteRepository。
然后我制作了一个 NoteController 并且我已经制作了 Get 和 Create 方法,但是我似乎无法弄清楚要编写什么来制作 Edit 和 Delete 方法?希望大家帮帮忙。
在这里您将看到我项目中的一些代码:
[HttpPost]
public ActionResult Create(MyNote mn)
{
try
{
note.Create(mn);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
这是从控制器创建的。
public static List<MyNote> notes = new List<MyNote>();
public NoteRepository()
{
notes.Add(new MyNote() { ID = 1, Titel = "Morgenmad", OprettelsesDato = DateTime.Now, Note = "Spis morgenmad i dag" });
notes.Add(new MyNote() { ID = 2, Titel = "Frokost", OprettelsesDato = DateTime.Now, Note = "Spis frokost i dag" });
notes.Add(new MyNote() { ID = 3, Titel = "Aftensmad", OprettelsesDato = DateTime.Now, Note = "Spis aftensmad i dag" });
}
public void Create(MyNote mn)
{
notes.Add(mn);
}
这里是存储库 class,其中包含创建方法的列表和方法。
请问我是否遗漏了什么!谢谢:-)
您似乎正在为 in-memory 存储库使用列表。对于删除,你可以这样实现:
public bool Delete (MyNote noteToDelete) {
return notes.Remove(noteToDelete);
}
编辑:但是,在这种情况下,列表将检查引用是否相等。因为你有一个 ID,我假设它是唯一的,你可以改为这样做:
public bool Delete(MyNote noteToDelete) {
var matchingNote = notes.FirstOrDefault(n => n.ID == noteToDelete.ID);
return notes.Remove(matchingNote);
}
您还可以在您的 MyNote class 上实施 IEquatable 以更改您的笔记相互比较的方式,并且 return 当 ID 相同时有效匹配。
对于 IEquatable 示例,您可能希望将 MyNote 的 class 定义更改为:
public class MyNote : IEquatable<MyNote>
并在MyNote中添加以下代码class:
public override bool Equals(object obj) {
if (obj == null) return false;
Part objAsNote = obj as MyNote;
if (objAsNote == null) return false;
else return Equals(objAsNote);
}
public bool Equals(MyNote otherNote) {
if(otherNote == null) return false;
return (this.ID.Equals(otherNote.ID));
}
public override int GetHashCode(){
return this.ID;
}
你可以这样做:
public ActionResult Edit(MyNote noteToEdit)
{
var oldNote = notes.FirstOrDefault(n => n.Id == noteToEdit.Id);
if(oldNote == null)
return View(); //With some error message;
oldNote.Title = noteToEdit.Title;
oldNote.OprettelsesDato = DateTime.Now;
oldNote.Note = noteToEdit.Note;
return RedirectToAction("Index", "Note");
}
public ActionResult Delete(int id)
{
var noteToRemove = notes.FirstOrDefault(x => x.Id == id);
if(noteToRemove == null)
return View(); //With some error message;
notes.Remove(noteToRemove);
return RedirectToAction("Index", "Note");
}
当您在编辑笔记时,我建议您使用AutoMapper,使您的代码更易于维护。
我在不使用数据库的情况下构建一个简单的 MVC CRUD,只是在 Repository 模型中创建方法 class。
为了更容易理解,我有 2 个模型 classes。我有一些属性的 MyNote 和有属性列表的 NoteRepository。
然后我制作了一个 NoteController 并且我已经制作了 Get 和 Create 方法,但是我似乎无法弄清楚要编写什么来制作 Edit 和 Delete 方法?希望大家帮帮忙。
在这里您将看到我项目中的一些代码:
[HttpPost]
public ActionResult Create(MyNote mn)
{
try
{
note.Create(mn);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
这是从控制器创建的。
public static List<MyNote> notes = new List<MyNote>();
public NoteRepository()
{
notes.Add(new MyNote() { ID = 1, Titel = "Morgenmad", OprettelsesDato = DateTime.Now, Note = "Spis morgenmad i dag" });
notes.Add(new MyNote() { ID = 2, Titel = "Frokost", OprettelsesDato = DateTime.Now, Note = "Spis frokost i dag" });
notes.Add(new MyNote() { ID = 3, Titel = "Aftensmad", OprettelsesDato = DateTime.Now, Note = "Spis aftensmad i dag" });
}
public void Create(MyNote mn)
{
notes.Add(mn);
}
这里是存储库 class,其中包含创建方法的列表和方法。
请问我是否遗漏了什么!谢谢:-)
您似乎正在为 in-memory 存储库使用列表。对于删除,你可以这样实现:
public bool Delete (MyNote noteToDelete) {
return notes.Remove(noteToDelete);
}
编辑:但是,在这种情况下,列表将检查引用是否相等。因为你有一个 ID,我假设它是唯一的,你可以改为这样做:
public bool Delete(MyNote noteToDelete) {
var matchingNote = notes.FirstOrDefault(n => n.ID == noteToDelete.ID);
return notes.Remove(matchingNote);
}
您还可以在您的 MyNote class 上实施 IEquatable 以更改您的笔记相互比较的方式,并且 return 当 ID 相同时有效匹配。
对于 IEquatable 示例,您可能希望将 MyNote 的 class 定义更改为:
public class MyNote : IEquatable<MyNote>
并在MyNote中添加以下代码class:
public override bool Equals(object obj) {
if (obj == null) return false;
Part objAsNote = obj as MyNote;
if (objAsNote == null) return false;
else return Equals(objAsNote);
}
public bool Equals(MyNote otherNote) {
if(otherNote == null) return false;
return (this.ID.Equals(otherNote.ID));
}
public override int GetHashCode(){
return this.ID;
}
你可以这样做:
public ActionResult Edit(MyNote noteToEdit)
{
var oldNote = notes.FirstOrDefault(n => n.Id == noteToEdit.Id);
if(oldNote == null)
return View(); //With some error message;
oldNote.Title = noteToEdit.Title;
oldNote.OprettelsesDato = DateTime.Now;
oldNote.Note = noteToEdit.Note;
return RedirectToAction("Index", "Note");
}
public ActionResult Delete(int id)
{
var noteToRemove = notes.FirstOrDefault(x => x.Id == id);
if(noteToRemove == null)
return View(); //With some error message;
notes.Remove(noteToRemove);
return RedirectToAction("Index", "Note");
}
当您在编辑笔记时,我建议您使用AutoMapper,使您的代码更易于维护。