Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:33:09+00:00 2026-05-30T11:33:09+00:00

I find half solutions everywhere but I cannot get a real answer for this.

  • 0

I find half solutions everywhere but I cannot get a real answer for this.

I have created a validation attribute inheriting from ValidationAttribute and IClientValidatable.

Everything seems to work fine, and if I put a validation summary on the page the error displays.

however I cannot get the error displaying beside the textbox relating to the property.

I think this is because I need to add the property name to the validation result.

HOWEVER this is where it fails, because I cannot get the member name from the validationContext.

is there a way to enable this to happen? Or am I missing something?

public class AdmissionDayCaseAttribute : ValidationAttribute, IClientValidatable
{
    private String DayCaseID;
    private String AdmissionDateID;
    private String DischargeDateID;

    public AdmissionDayCaseAttribute(String DayCaseID, String AdmissionDateID, String DischargeDateID)
    {
        this.DayCaseID = DayCaseID;
        this.AdmissionDateID = AdmissionDateID;
        this.DischargeDateID = DischargeDateID;
    }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        object AdmissionDate = GetPropertyValue(validationContext.ObjectInstance, AdmissionDateID);
        object DischargeDate = GetPropertyValue(validationContext.ObjectInstance, DischargeDateID);
        object AdmissionType = validationContext.MemberName == null ? null : GetPropertyValue(validationContext.ObjectInstance, validationContext.MemberName);
        if (AdmissionDate != null && DischargeDate != null && AdmissionType != null)
        {
            if (AdmissionDate.Equals(DischargeDate) && DayCaseID.Equals(AdmissionType.ToString()))
            {
                return new ValidationResult(ErrorMessage, new List<String>() { validationContext.MemberName });
            }
        }
        return ValidationResult.Success;
    }
    private object GetPropertyValue(object instance,String propertyName)
    {
        Type itype = instance.GetType();
        return itype.GetProperty(propertyName).GetValue(instance,null);
    }
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ModelClientValidationRule vr = new ModelClientValidationRule()
        {
            ErrorMessage = ErrorMessage,
            ValidationType = "admissiondaycase"
        };
        vr.ValidationParameters.Add(new KeyValuePair<string, object>("admissiondateid", AdmissionDateID));
        vr.ValidationParameters.Add(new KeyValuePair<string, object>("dischargedateid", DischargeDateID));
        vr.ValidationParameters.Add(new KeyValuePair<string, object>("daycaseid", DayCaseID));
        yield return vr;
    }

EDIT/UPDATE

the view looks like this –

<tr>
    <td>
        @Html.LabelFor(x => x.AdmissionTypeID)
    </td>
    <td>
        @Html.CustomDropDownListFor(a => a.AdmissionTypeID, Lookup.AdmissionTypes,Default.Constant.DROPDOWN)
        @Html.ValidationMessageFor(a=>a.AdmissionTypeID)
    </td>
</tr>

There no javascript or anything interacting with this, its pretty basic however some extra details –
this view code is in a view template, which is used IN a partial view

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:33:11+00:00Added an answer on May 30, 2026 at 11:33 am

    First check that the AdmissionDayCaseAttribute is correctly definied on the property in your model.

    Then instead of returning validationContext.MemberName return validationContext.DisplayName. If you modified the member name through the DsiplayAttribute the you could do something like this :

      var memberName = validationContext.ObjectType.GetProperties()
        .Where(p => p.GetCustomAttributes(false)
        .OfType<DisplayAttribute>()
        .Any(a => a.Name == validationContext.DisplayName))
        .Select(p => p.Name).FirstOrDefault(); 
    
        return new ValidationResult(ErrorMessage, new List<String>() { memberName  });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off: I searched half the web to find an answer with this as
Ok, I've been trying to find a solution for this for about half a
I've now read kinda half of the Doctrine 2 documentation but I can't find
We've been coping with this issue for quite some time and find only half
I just spent half an one our to find out what caused the Error-Message
find whether there is a loop in a linked list. Do you have other
I find that getting Unicode support in my cross-platform apps a real pain in
I've spent half day trying to figure out this and finally I got working
Ok, i know it has been asked several times but i didn't find a
I realize this type of question has been asked earlier, however I looked everywhere

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.