asp.net-mvcumbracoumbraco7umbraco6umbraco-contour

Use UIOMaticField or backoffice controls in Ui-O-Matic Umbraco


I am new to Umbraco CMS. I am using Ui-O-Matic plugin in my project.
Ui-O-Matic allows easy CRUD operation for database. But I want to use backoffice controls like file, textarea, etc.

So I am using UIOMaticFielld like this in database.cs file.

 [Column("newsDetail")]
 [UIOMaticField("News Detail","Add Details",View ="textarea")]
 public string newsDetail { get; set; }

 [Column("newsImage")]
 [UIOMaticField("Image","Upload Image",View ="file")]
 public string newsImage { get; set; }

Problem is when I make any change in database, I have to refresh database.tt file to get database changes. But it recreates database.cs file and my previous changes:

[UIOMaticField("News Detail","Add Details",View ="textarea")]

removes from database.cs file. And every time I have to do the same changes.
Please guide me what should I do to keep my custom changes as it is even I refresh database.tt file?
Other better way to do CRUD operation is also preferable.


Solution

  • After googling a lot, I found that as database.cs file is auto generated, I must not do custom changes in it.
    I found another way to use backoffice controls. Let me explain here, may it will help to other.

    Instead of writing UIOMatoicField in databse.cs file, create model to do the same.
    Make below changes in "Models/Generated/database.tt" file

    // Settings 
        ConnectionStringName = "umbracoDbDSN";          // Uses last connection string in config if not specified
        Namespace = "Generator";
        RepoName = "";
        GeneratePocos = true;
        ClassPrefix = "";  
        ClassSuffix = "";  
    
        // Read schema 
        var tables = LoadTables();  
        tables["Course"].Ignore = true; // Prevents table to include in databse.cs file  
        tables["News"].Ignore = true; 
    if (tables.Count>0)
        {
    #>
    <#@ include file="UIOMatic.Generator.ttinclude" #>
    <# } #>
    

    Then create new Model as below. For ex. "Models\NewsModel.cs"

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using UIOMatic.Attributes;
    using UIOMatic.Enums;
    using UIOMatic.Interfaces;
    using Umbraco.Core.Persistence;
    using Umbraco.Core.Persistence.DatabaseAnnotations;
    
    namespace EdumentUIOMatic.Models
    {
        [Umbraco.Core.Persistence.TableName("News")]
        [PrimaryKey("newsId")]
        [UIOMatic("News", "icon-box-open", "icon-box-open", RenderType = UIOMaticRenderType.List, ConnectionStringName = "umbracoDbDSN")]   
        public class NewsModel: IUIOMaticModel
        {
            [UIOMaticIgnoreField]
            [Column("newsId")]
            public int newsId { get; set; }
    
            [Column("newsTitle")]
            [UIOMaticField("News Title", "Add Title")]
            public string newsTitle { get; set; }
    
            [Column("newsDetail")]
            [UIOMaticField("News Detail", "Add Details", View = "textarea")]
            public string newsDetail { get; set; }
    
            [Column("newsImage")]
            [UIOMaticField("Image", "Upload Image", View = "file")]
            public string newsImage { get; set; }
    
    
    
            [Column("isDeleted")]
            [UIOMaticField("Hide News", "Check if you want to hide this news")]        
            public bool isDeleted { get; set; }
    
    
    
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            public IEnumerable<Exception> Validate()
            {
                return new List<Exception>();
            }
        }
    }
    

    Now if you will reload database.tt file to get updated database, your code will not be removed.