[CLOSED] mvc data annotation not working ?

  1. #1

    [CLOSED] mvc data annotation not working ?

    Hi, I try to set display name and date format, error message using data annotation, but it seems the settings were ignored. Any idea?

    Thanks
    -szhang

    This is my code example:
    @model IEnumerable<CPMExtnet.ViewModels.EmployeePlaceholder>
    
    @section content
    {
       
     @(X.Viewport().Layout(LayoutType.Fit)
         .Items (
            X.GridPanel()
              .ID("EmployeePlaceholderGrid")
              .Title ("Your Employee Placeholders")
              .Icon(Icon.Table)
              .Frame(true)
              .ForceFit(true)
              .AutoScroll(true)
           
             .Store(
                   X.StoreForModel().Control (ctl =>
                    {
                        ctl.AutoSync = true;
                        ctl.Proxy.Add(
                            new RestProxy
                            {
                                AppendAction = false,
                                Reader = { 
                                    new JsonReader {
                                        Root = "data",
                                        MessageProperty = "message"
                                    }
                                },
                                API =
                                {
                                    Read = Url.Action("ReadEmployeePlaceholders"),
                                    Update = Url.Action("UpdateEmployeePlaceholder"),
                                    Create = Url.Action("CreateEmployeePlaceholder")
                                },
                                Writer = {
                                    new JsonWriter
                                    {
                                        AllowSingle = true
                                    }
                                }
                            }
                        );
                    })
              )
              .ColumnModel(
                  X.ColumnFor(Model, m=> m.PEID),   
                  X.ColumnFor (Model, m=> m.Name)
                     .ToBuilder<Column.Builder>()
                     .Editor(
                        X.TextField().AllowBlank(false)
                     ),
                 X.ColumnFor(Model, m=> m.BasePayStartDate)
                  .ToBuilder<Column.Builder>()
                  .Editor (
                      X.DateField().AllowBlank(false)
                      )
               )
             .TopBar(
                    Html.X().Toolbar()
                        .Items(
                            Html.X().Button()
                                .Text("Add")
                                .Icon(Icon.Add)
                                .Handler("InsertEmployeePlaceholder (this.up('grid'));"),
    
                        )
                )
                .BottomBar(X.PagingToolbar())
                .SelectionModel( X.RowSelectionModel().Mode(SelectionMode.Single))
                .Plugins(
                        X.RowEditing()
                            .Listeners(l => {
                                l.CancelEdit.Handler = "if(e.record.phantom){e.store.remove(e.record);}";
                 
                            })
                )
                 
    
    this the model code:
    
    
     [Model(Name = "EmployeePlaceholder")]
        public class EmployeePlaceholder
        {
    
            [ModelField(IDProperty = true, UseNull = true)]
            [Field(Ignore = true)]
            public int? Id { get; set; }
    
    
            public string PEID  { get;  set; }
    
            [Required]
            [StringLength(40)]
            [PresenceValidation]
            public string Name {get; set; }
    
    
            [Required]
            [PresenceValidation]
            public string PositionCode {get; set;}
    
            [Required(ErrorMessage="Start Date is required")]     //instead showing error I defined here,  it shows default error message  
            [PresenceValidation]
            [DataType (DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:d}")]       // never change the date format
            [Display(Name="Start Date")]                            // it show column name instead 
            public DateTime? BasePayStartDate { get; set; }
    
            ..
    Last edited by Daniil; Jan 21, 2015 at 12:51 PM. Reason: [CLOSED]
  2. #2
    Hello,

    Can you please take a look at the following discussion?

    http://forums.ext.net/showthread.php...-for-gridpanel
  3. #3
    Checked on the site, my issue is different. Grid panel validation works but it does not show the error as defined, it shows default message :"The filed is required", also the column header is displayed using model variable name not the the Display Name defined in annotation, the start date is not displayed in {0,d} format.

    Is there a way to define header text & format on client side with X.ColumnFor(Model,,) ?
    Thanks
    -szhang
    Last edited by susanz; Jan 13, 2015 at 4:30 PM.
  4. #4
    1. This works for me.
    [Display(Name = "Start Date")]
    You might need to update to get it working.

    2. Please use
    [DateColumn(Format="d")]
    instead of
    [DisplayFormat(DataFormatString = "{0:d}")]
    3. Unfortunately, this doesn't go to a Column's Editor.
    [Required(ErrorMessage="Start Date is required")]
    Please use a DateField's BlankText or you can use this:
    Html.X().ColumnFor(m => m.BasePayStartDate)
        .ToBuilder<DateColumn.Builder>()
        .Editor(Html.X().DateFieldFor(m => m.BasePayStartDate).FieldLabel(""))
    Hope this helps.
  5. #5
    Daniil, Thanks for your advice! The error message is working fine now, I'll check header text later.

    I tried
     [DateColumn(Format="d")]
    it works fine for non editable column but I got

    "Object reference not set to an instance of an object." error for editable column.

    The exception was thrown while constructing gridpanel.

    I got same error if I change to use DateColumn builder.

    Html.X().ColumnFor(m => m.BasePayStartDate)
        .ToBuilder<DateColumn.Builder>()
        .Editor(Html.X().DateField())
    any idea?

    Do you think upgrade to 3.x can help ?
    Thanks

    -szhang
    Last edited by Daniil; Jan 15, 2015 at 6:35 AM. Reason: Please use [CODE] tags
  6. #6
    Please don't forget to use [CODE] tags wrapping any code that you post.

    I cannot reproduce the exception. Here is my test case. Please provide your one.

    View
    @model Work2MVC.Controllers.Test
    
    @{
        var X = Html.X();    
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>
    </head>
    <body>
        @X.ResourceManager()
    
        @(X.GridPanel()
            .Width(500)
            .Height(200)
            .Store(X.Store()
                .Model(X.Model()
                    .Fields(X.ModelField()
                        .Name("StartDate")
                        .Type(ModelFieldType.Date)
                    )
                )
                .DataSource(ViewBag.Data as List<Work2MVC.Controllers.Test>)
            )            
            .ColumnModel(
                X.ColumnFor(m => m.Date)
                    .ToBuilder<DateColumn.Builder>()
                    .Editor(X.DateFieldFor(m => m.Date).FieldLabel(""))
            )
            .Plugins(X.CellEditing())
        )
    </body>
    </html>
    Controller
    namespace Work2MVC.Controllers
    {
        public class Test
        {
            [DateColumn(Format = "d")]
            [Display(Name = "Start Date")]
            [Required(ErrorMessage = "Start Date is required")]
            public DateTime Date
            {
                get;
                set;
            }
    
            public static List<Test> GetAll()
            {
                return new List<Test> 
                { 
                    new Test { Date = DateTime.Now },
                    new Test { Date = DateTime.Now },
                    new Test { Date = DateTime.Now }
                };
            }
        }
    
        [DirectController]
        public class RazorController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Data = Test.GetAll();
                return this.View();
            }
        }
    }
  7. #7
    Thank Daniil. I am working on other things now. will take detail look on this issue later. I'll let you know.
    Thanks
    -szhang
  8. #8
    Here is a related thread.
    http://forums.ext.net/showthread.php?59780

Similar Threads

  1. [CLOSED] Range data annotation not working with Textfiled
    By PriceRightHTML5team in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 21, 2014, 1:18 PM
  2. Replies: 2
    Last Post: Jul 25, 2013, 7:30 AM
  3. [CLOSED] Data annotation validation in form
    By MTSI in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Mar 05, 2013, 2:04 PM
  4. Replies: 5
    Last Post: Dec 06, 2012, 3:42 PM
  5. Data annotation& validation sample
    By nurkmez in forum 2.x Help
    Replies: 1
    Last Post: Nov 24, 2012, 6:57 PM

Posting Permissions