1

I have a View model

public class TrainingProgramScheduledDateVM
  {

public bool IsTuesday { get; set; }

    [DataType(DataType.Time)]
    public string TueStartTime { get; set; }
    [DataType(DataType.Time)]
    public string TueEndTime { get; set; }

    public bool IsWednesday { get; set; }

    [DataType(DataType.Time)]
    public string WedStartTime { get; set; }
    [DataType(DataType.Time)]
    public string WedEndTime { get; set; }

    public bool IsThursday { get; set; }

    [DataType(DataType.Time)]
    public string ThuStartTime { get; set; }
    [DataType(DataType.Time)]
    public string ThuEndTime { get; set; }

  }

I want to put the validation i a way that. 1- If IsTuesday = true Then TueStartTime and TueEndTime should Requires. and so on for other

2- At least one boolean value should be true and relevant time should be required.

please suggest me any easy way.

Thanks

1

2 Answers 2

1

Use remote validation. For example, Here

        [Remote(
        "doesProductNameExistUnderCategory", 
        "Northwind", 
        AdditionalFields = "Category_ID",
        ErrorMessage = "Product name already exists under the chosen category. Please enter a different product name.",
        HttpMethod = "POST"
    )]
    [Required]
    public string Product_Name { get; set; }
Sign up to request clarification or add additional context in comments.

6 Comments

How is that going to help with posting and validating a new model?
Can you please send the proper example.
Complete sample here: tugberkugurlu.com/archive/…
@CodeCaster: remote validation can help you to implement very sophisticated validation: by two, three fields. It also allows to validate data by additional data from database. This is not only way, but I like this approach.
sounds good. but not suits on my case, i dont want to be post back for just validation.
|
0

I see repetition. This block:

public bool IsTuesday { get; set; }

[DataType(DataType.Time)]
public string TueStartTime { get; set; }
[DataType(DataType.Time)]
public string TueEndTime { get; set; }

Can better be abstracted into its own type, like:

public class ScheduleDate
{
    public DayOfWeek DayOfWeek { get; set; }

    public bool IsSelected { get; set; }

    [DataType(DataType.Time)]
    [RequiredIf(IsSelected)]
    public string StartTime { get; set; }

    [DataType(DataType.Time)]
    [RequiredIf(IsSelected)]
    public string EndTime { get; set; }
}

Then your TrainingProgramScheduledDateVM can contain a list of ScheduleDate objects.

3 Comments

I think it will not make sense. can you please send the example ?
@SirajHussain I cannot send an example, you will have to implement this in your code.
In what assembly is defined RequiredIfAttribute? I can't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.