Setting up a basic editor

Find the demo project in https://github.com/neville-nazerane/NetCore.Simple.demo

The easiest way to take advantage of the built in editor is by first creating a model that implements the IBasicModel interface. This interfact consists of four basic straight forward functions for add, update, get and delete. The model can also contain all required validation attributes that will be used by the editor.

Once this model with the functions implemented, this model can be used to create a controller that extends the BasicController. The following example for a model Person

public class PersonController : BasicController
{
    
}

The above class creates the complete controller required for the add, update and delete functionalities. Although this class does not require a body and will still have full functionality, all four actions of this controller can be overridden if needed.

Once the controller is created, a view for the editor can now be created. By default the main view that will be used would be a view named Index.cshtml inside the folder of the controller (in this example inside a folder named "person"). If you would like to have a different name for your file for any reason, your custom file name needs to be provided to the controller as follows:

public class PersonController : BasicController
{
    public PersonController(){
        ViewName = "myCustomName";
    }
}

Before creating the main view, you will need to set up a partial view that would contain the template for the editor. This template would mainly use the editorLayout and include the text fields required.


@{ 
    Layout = "netcore.simple/_editorLayout";
    var i = Html.GetInputGenerator(); 
}

@i.Make(p => p.FirstName)

@i.Make(p => p.LastName)

The first line sets the editor layout as the default layout. Then a variable i is created that acts as an input generator for the Person object. The next two lines of codes are simply using the input generator to generate strongly typed input fields.

Once the editor partial view is created, the main view can now be created with NetCore.Simple.Models.Editor set as its model type. Now this view can render the editor partial view and by passing on the model. Now the main view (Index.cshtml by default) will have a function as follows


@model NetCore.Simple.Models.Editor

@Html.Partial("PersonEditor", Model)

Now you can add onto this view any more information you prefer including the list of enteries. Check the git repository for a complete example on the Index.cshtml file. For updating a record, the url would be "http://domain/controller?id=123". In this example, it would be "http://domain/person?id=123". The parameter(s) "id=123" can be anything based on your model. These parameters would contain the unique data that is required by the Get() function in the model to fetch the record data. Here is an example of how the url redirect could be set up.


@foreach (var p in Person.GetAll())
    {
        <tr>
            <td><a href="~/Person/@p.ID">@p.ID</a></td>
            <td>@p.FirstName</td>
            <td>@p.LastName</td>
        </tr>
    }

This url redirects to the default view and populates the current entrie's data into the respective input fields.