Ramco Ramco

Ramco Ramco

  • 439
  • 3.4k
  • 545.1k

Data Annotation Value not getting implemented

Jun 18 2021 10:59 AM

Hi

   I have below Index View 

@using (Html.BeginForm("CreateEdit", "Customer", FormMethod.Post))
        {
            @Html.AntiForgeryToken()
            <input type="hidden" id="hfAU" name="hfAU" value="A" />
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            @*<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>Close</button>*@
                            <h4 class="modal-title"></h4>
                        </div>
                        <div class="modal-body" id="myModalBody">
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary">Save Changes</button>
                        </div>
                    </div>
                </div>
            </div>
        }

******************************** JAVASCRIPT 

$('body').on('click', '[id*=btnEdit]', function () {
        hfAU = "U";
        var data = $(this).parents('tr').find('td');
        var Id = data.eq(0).html();
        $('#hfId').val(Id);
        var url = "/Customer/CreateEdit?Id=" + Id;
        $("#myModalBody").load(url, function () {
            $(".modal-title").html("Update");
            $("#myModal").modal("show");
        });
    });

********************************************* DATA ANNOTATION

    public class Customer
    {
        //private string _isactive = "Y";
        public Customer()
        {
            CreatedOn = DateTime.Now;
            //LastUpdatedOn = DateTime.Now;
        }

        [Key]
        [Required(ErrorMessage = "Id cannot be Blank. Max length should be less than or equal to 20."), MaxLength(20)]
        //[RegularExpression(@"^[ a-zA-Z\s]$", ErrorMessage = "Special Characters not allowed")]
        [Display(Name = "Id")]
        [Remote("IsExist", "Customer", ErrorMessage = "Id already exists")]
        public string Id { get; set; }

        [Display(Name = "Description")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Customer Name cannot be Blank. Max length should be less than or equal to 50"), MaxLength(50)]
        public string Description { get; set; }

        [Display(Name = "Active")]
        public bool IsActive { get; set; }

        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime CreatedOn { get; set; }

    }


Answers (7)