0

I am working on a survey application by using asp.net mvc, bootstrap, jquery unobtrusive validation. I have a validation problem. My table in my database is as follows. I have 2 nonnullable datetime columns.

CREATE TABLE [dbo].[Surveys] (
[SurveyId]          INT          IDENTITY (1, 1) NOT NULL,
[SurveyName]        VARCHAR (50) NOT NULL,
[SurveyDescription] VARCHAR (50) NULL,
[CreatedDate]       DATETIME     NOT NULL,
[UpdatedDate]       DATETIME     NOT NULL,
[CreatedUserId]     INT          DEFAULT ((0)) NOT NULL,
[IsActive]          BIT          DEFAULT ((0)) NOT NULL,
[Status]            BIT          DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK__Surveys__A5481F7DFAE1437E] PRIMARY KEY CLUSTERED ([SurveyId] ASC)
);

There is a survey table on the survey display page. When I press the update button on each row in this table, I open a popup. In this popup, only the survey name and survey description can be updated. Validation should only be done for these two areas. Date fields are for informational purposes only and are disabled. The problem is this; When you press submit button for update, jquery unobtrusive tries to validate the disabled date fields. This is not desirable. How do I solve this problem?

As you can see, when you press submit button, a red frame is formed around the date field.

img

Surveys.cs

// <auto-generated>


using System.ComponentModel.DataAnnotations;

namespace MerinosSurvey.Models
{
using System;
using System.Collections.Generic;

public partial class Surveys
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Surveys()
    {
        this.SurveyQuestions = new HashSet<SurveyQuestions>();
        this.SurveyCustomers = new HashSet<SurveyCustomers>();
    }

    public int SurveyId { get; set; }
    [Required(ErrorMessage = "Lütfen anket adını giriniz.")]
    public string SurveyName { get; set; }
    [Required(ErrorMessage = "Lütfen anket açıklamasını giriniz.")]
    public string SurveyDescription { get; set; }

   // [DataType(DataType.Date)]
    public System.DateTime? CreatedDate { get; set; }
    //[DataType(DataType.Date)]

    public System.DateTime? UpdatedDate { get; set; }
    public int CreatedUserId { get; set; }
    public bool IsActive { get; set; }
    public bool Status { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SurveyQuestions> SurveyQuestions { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SurveyCustomers> SurveyCustomers { get; set; }
}
}

AddOrEdit.cshtml (popup)

.
.
.

 <div class="form-group row">
    @Html.Label("CreatedDate", "Created Date", new { @class = "col-form-label col-md-3" })
    <div class="col-md-9">
        @Html.TextBoxFor(model => model.CreatedDate, "{0:dd/MM/yyyy}",new { @class = "form-control", type = "date-picker", value = Model.CreatedDate } )
    </div>
</div>

<div class="form-group row">
    @Html.Label("UpdatedDate", "Updated Date", new { @class = "col-form-label col-md-3" })
    <div class="col-md-9">
        @Html.TextBoxFor(model => model.UpdatedDate, "{0:dd/MM/yyyy}", new { @class = "form-control", type = "date-picker", value = Model.UpdatedDate })

    </div>
</div>

.
.
.
  • try to override `data_val` attribute, e.g. `@Html.TextBoxFor(model => model.CreatedDate, "{0:dd/MM/yyyy}",new { @class = "form-control", type = "date-picker", value = Model.CreatedDate, data_val = "false" })` as explained here https://stackoverflow.com/a/47336349/2516718 – derloopkat Jan 12 '20 at 20:11
  • @derloopkat buddy thank you. I passed client-side-validation by adding data_val = false. However, when using disabled = "disabled" because the date fields and id fields are not changeable, these 3 fields pass null to asp.net mvc controller, server-side validation fails. when I change these fields to readonly = "readonly", it works without problems. But when you readonly the cursor focuses on these areas. I wonder what the best practice is. Is there a problem with my method? –  Jan 12 '20 at 20:47
  • then I want to accept your answer and upvote if you post it as an answer. If you like my question, you can upvote it. –  Jan 12 '20 at 21:33
  • Thanks, but your question seems to be a duplicate of [Disable Validation on a single Html.EditorFor](https://stackoverflow.com/questions/34789983/disable-validation-on-a-single-disabled-html-editorfor). In these cases we have to flag it, not answer. – derloopkat Jan 12 '20 at 21:51
  • ohh yes you are right it look like my problem.Thanks again. I am wondering Are you curious about the Datatable structure? I'd like to discuss a problem with datatable. If you want we talk in chat the Datatable issue. –  Jan 12 '20 at 22:00
  • Sorry, I'm leaving now. You can try asking new DB question here in Stackoverflow or https://dba.stackexchange.com. – derloopkat Jan 12 '20 at 22:27

0 Answers0