Step 4.3. CRUD logic

Step 4.3. CRUD logic

Skip and go to the next step

On this step we must specify how data requests will be proccessed and send back to the server.

Our implementation will contain the following:

  • checking user rights;
  • proccesing data requests;
  • updating data on the client-side.

In the end we'll return the Index() action that redirects us to the Employee view.

Your actions:

  1. Move to Solution Explorer→Controllers and open the SystemController.cs file.
  2. Add the Save() action to the existing code of the file:
public ActionResult Save(Task task) {  
       var action = new DataAction(this.Request.Form);// an action against particular task (updated/deleted/created) 
 #region check rights
       if (!RoleIs("Manager"))
       {
           action.Type = DataActionTypes.Error;
           return new AjaxSaveResponse(action);
       }
 #endregion
       TasksDataContext data = new TasksDataContext();
       try
       {
           switch (action.Type)
           {
                case DataActionTypes.Insert:                       
                    data.Tasks.InsertOnSubmit(task);
                    break;
                case DataActionTypes.Delete:
                    task = data.Tasks.SingleOrDefault(ev => ev.id == action.SourceId);
                    data.Tasks.DeleteOnSubmit(task);
                    break;
                default:// "update"                          
                    task = data.Tasks.SingleOrDefault(ev => ev.id == action.SourceId);
                    UpdateModel(task);
                    break;
            }
            data.SubmitChanges();
            action.TargetId = (long)task.id;
        }
        catch (Exception a)
        {
            action.Type = DataActionTypes.Error;
        }
 
        var color = data.Statuses.SingleOrDefault(s => s.key == task.status_id);
 
        var result = new AjaxSaveResponse(action);
        result.UpdateField("color", color.color);
        return result;  
}

RoleIs() is the function that we defined on the step 3 (sub-step 3.2.3).



Please, pay attention to the following information!

The Save action takes a Task object as the parameter. Task objects are generated automatically, using MVC model binder: the binder takes values from the related request, converts them to the appropriate data type and assigns to the model properties. The binder expects date to be sent in the current culture info date format and if it differs from the default scheduler format ('%m/%d/%Y %H:%i'), date will be converted incorrectly. To exclude such a possibility and ensure correct data processing, the scheduler date format must be set manually.

In case date format for your locale is 'dd/mm/yyyy', we specify the related date configuration options in the following actions:

  1. Manager() - 'Step 4.1.3 Manager action. Scheduler configuration'

    scheduler.Config.xml_date = "%d/%m/%Y %H:%i";
  2. Data() - 'Step 4.2 Loading data to the scheduler'

    var resp = new SchedulerAjaxData(tasks);
    resp.DateFormat = "%d/%m/%Y %H:%i";
  3. Employee() - 'Step 5.1.3 Employee action. Scheduler configuration'

    scheduler.Config.xml_date = "%d/%m/%Y %H:%i";




comments powered by Disqus