ASP.NET MVC
ASP.NET MVC is a powerful framework that provides the ability to create complex dynamic web-sites. Its extensibility makes it easy to add DataTables and Editor to your toolbox, providing an even smoother experience for your users.
Installation
The first step to using the Editor libraries in your MVC project is to include a reference to the DataTables-Editor-Server.dll
, which provides all the classes and methods required. If you haven't already please follow the Installation instructions to include the DLL and also set up the database connection.
To complete the installation, add a using
statement to the files where you will be using the Editor components (typically the model and controller):
using DataTables;
Controller
Editor can be used in an MVC project in the same manner as any other MVC component by extending the Controller
class. The controller defines the HTTP access point by the class name and routing that has been configured (note that attribute routing can also be used if that is your preference and more generally the ApiController
class can be used if you are using Web API - see the Editor Web API documentation).
Consider the following typical MVC controller that operates with Editor:
[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
public class StaffController : Controller
{
public ActionResult Table()
{
var settings = Properties.Settings.Default;
var formData = HttpContext.Request.Form;
using (var db = new Database(settings.DbType, settings.DbConnection))
{
var response = new Editor(db, "staff")
.Model<StaffModel>()
.Field(new Field("start_date")
.Validator(Validation.DateFormat(
Format.DATE_ISO_8601,
new ValidationOpts { Message = "Please enter a date in the format yyyy-mm-dd" }
))
.GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
.SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601))
)
.Process(formData)
.Data();
return Json(response, JsonRequestBehavior.AllowGet);
}
}
}
Where:
- Line 1-3: Define the controller and method, thus giving the HTTP URL (when using the default routing in MVC) as
staff/table
. - Line 5: Create variable to easily access database connection properties - this is not required, you can access this information from any other location that you might have it in your application.
- Line 7: Create a new
Database
instance in anusing
statement - Line 9: Created a new
Editor
instance using the databasestaff
. - Line 10: Uses the
StaffModel
to define the fields - Lines 11-14: Add a validator for the
start_date
field. Other fields can also have suitable validators added - Lines 15-16: Use get and set formatters for the date field
- Line 18: Processes the data from
Request.Form
(which is defined by theController
class) - Line 19-21: Get the JSON response and return it to the client.
Please note that the Table()
method defined above will process all four CRUD actions (Create, Read, Update and Delete). As such the ajax
and ajax
options for DataTables and Editor would both be set to point at this method (i.e. /staff/table
). If you prefer to use REST URLs, that is also possible using the ajax
option.
Models
For information about the models used for the Model()
method, please refer to the getting started model documentation.
Next steps
There is of course a lot more that you can do with the Editor libraries, although you will typically apply the basics shown in this article every time. To expand your use of Editor, consider the following topics: