I am working on a ASP.NET MVC project. I have a question about the View in a CRUD operation.
In most of the examples I have seen, separate views for each CRUD operation (e.g. Add, Edit, Delete) are used. Now imagine if I have 100 tables in my database, and each of them requires CRUD operations via a View. Is it best to create these separate Views for each table or create a function that would create these Views for me, such as below?
public ActionResult CreateSubject()
{
return View(Model.toList());
}
public ActionResult EditSubject()
{
return View();
}
public ActionResult DeleteSubject()
{
return View();
}
I use separate actions for each operation on my controller and create a simple PartialView that handles all the fields and then I use a shared View from my Shared folder that loads my partial view.
Supporting interfaces:
CreateModel.cs:
EditModel.cs:
_create.cshtml:
Create.cshtml:
Edit.cshtml
This is an example of how I handle my multiple CRUD operations (at least in terms of showing forms for them). There would obviously be more content in your Create/Edit views
By placing the Edit.cshtml and Create.cshtml in the Shared folder, it'll be used by default when you return a view from an action with those names. By default the view engine checks the appropriate view folder for the controller for the file and then looks to Shared. Each of your _create.cshtml partial should be in the appropriately named View folders and they will be delivered correctly.