title: Best way to implement a Require validation if another field is not null
body: <p>I have the following two fields inside my view:-</p>
  
  <pre><code>&lt;div&gt;
  
  &lt;span class="f"&gt;@Html.DisplayNameFor(model=&gt;model.Firewall.ConsoleServerID)&lt;/span&gt;
  
  @Html.DropDownListFor(model =&gt;model.Firewall.ConsoleServerID, ((IEnumerable&lt;IT.Models.ITConsoleServer&gt;)ViewBag.Servers).Select(option =&gt; new SelectListItem {
  Text = (option == null ? "None" : option.Technology.Tag), 
  Value = option.ConsoleServerID.ToString(),
  Selected = (Model != null) &amp;&amp; (Model.Firewall.ITConsoleServer != null) &amp;&amp; (option.ConsoleServerID == Model.Firewall.ConsoleServerID)
  }), "Choose...")
  @Html.ValidationMessageFor(model =&gt;model.Firewall.ConsoleServerID)
  
  &lt;/div&gt;
  
  &lt;div &gt;
  &lt;span class="f"&gt;@Html.DisplayNameFor(model=&gt;model.Firewall.ConsoleServerPort)&lt;/span&gt; 
  
  @Html.TextBoxFor(model =&gt; model.Firewall.ConsoleServerPort) 
  @Html.ValidationMessageFor(model =&gt; model.Firewall.ConsoleServerPort) | 
  </code></pre>
  
  <p></p>
  
  <p>The rule I want to implement is that both fields are NullAble, but if the user selects a ConsoleServerID , then he needs to add  a Port . and if the user leave the ConsoleServerID null then he should not enter a Port number.
  So what is the best way to implement this scenario , so that I have both client side and server side validation ?
  Thanks</p>
  
  <p><strong>Edit</strong>
  After installing the foolproof i am facing the following problems :-</p>
  
  <ol>
  <li><p>no client side validation is being implemented, and the validation is being implemented when checking if the model state is valid inside my controller class</p></li>
  <li><p>if the [RequiredIfNotEmpty] is false then the suer will recive the following message:- Console Server Port is required due to ConsoleServerID not being empty .so why ConsoleServerID will be displayed instead of the field name which is Console Server Tag.</p></li>
  <li><p>if the user tried to submit the form where the ConsoleServerID is null then i will recevie the following exception :-</p></li>
  </ol>
  
  <blockquote>
    <p>System.Data.Entity.Validation.DbUnexpectedValidationException was
    unhandled by user code   HResult=-2146233087   Message=An unexpected
    exception was thrown during validation of 'Console Server Port' when
    invoking Foolproof.RequiredIfNotEmptyAttribute.IsValid. See the inner
    exception for details.   Source=EntityFramework   StackTrace:
           at System.Data.Entity.Internal.Validation.ValidationAttributeValidator.Validate(EntityValidationContext
    entityValidationContext, InternalMemberEntry property)
           at System.Data.Entity.Internal.Validation.PropertyValidator.Validate(EntityValidationContext
    entityValidationContext, InternalMemberEntry property)
           at System.Data.Entity.Internal.Validation.EntityValidator.ValidateProperties(EntityValidationContext
    entityValidationContext, InternalPropertyEntry parentProperty, List<code>1
    validationErrors)
           at System.Data.Entity.Internal.Validation.TypeValidator.Validate(EntityValidationContext
    entityValidationContext, InternalPropertyEntry property)
           at System.Data.Entity.Internal.Validation.EntityValidator.Validate(EntityValidationContext
    entityValidationContext)
           at System.Data.Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary</code>2
    items)
           at System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry, IDictionary<code>2 items)
           at System.Data.Entity.DbContext.GetValidationErrors()
           at System.Data.Entity.Internal.InternalContext.SaveChanges()
           at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
           at System.Data.Entity.DbContext.SaveChanges()
           at TMS.Models.Repository.Save() in c:\Users\Administrator\Desktop\New folder
    (19)\TMS\TMS\TMS\Models\Repository.cs:line 4614
           at TMS.Controllers.FirewallController.Edit(FirewallJoin fj, FormCollection formValues) in c:\Users\Administrator\Desktop\New
    folder (19)\TMS\TMS\TMS\Controllers\FirewallController.cs:line 869
           at lambda_method(Closure , ControllerBase , Object[] )
           at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
           at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
    controllerContext, IDictionary</code>2 parameters)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor, IDictionary<code>2
    parameters)
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass42.&lt;BeginInvokeSynchronousActionMethod&gt;b__41()
           at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8</code>1.b__7(IAsyncResult
    _)
           at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
    asyncResult)
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;>c__DisplayClass37.&lt;>c__DisplayClass39.b__33()
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;>c__DisplayClass4f.b__49()
    InnerException: System.NotImplementedException
           HResult=-2147467263
           Message=The method or operation is not implemented.
           Source=MVC Foolproof Validation
           StackTrace:
                at Foolproof.ModelAwareValidationAttribute.IsValid(Object value)
                at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object
    value, ValidationContext validationContext)
                at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object
    value, ValidationContext validationContext)
                at System.Data.Entity.Internal.Validation.ValidationAttributeValidator.Validate(EntityValidationContext
    entityValidationContext, InternalMemberEntry property)
           InnerException:</p>
  </blockquote>