I have the following two fields inside my view:-
<div>
<span class="f">@Html.DisplayNameFor(model=>model.Firewall.ConsoleServerID)</span>
@Html.DropDownListFor(model =>model.Firewall.ConsoleServerID, ((IEnumerable<IT.Models.ITConsoleServer>)ViewBag.Servers).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.Technology.Tag),
Value = option.ConsoleServerID.ToString(),
Selected = (Model != null) && (Model.Firewall.ITConsoleServer != null) && (option.ConsoleServerID == Model.Firewall.ConsoleServerID)
}), "Choose...")
@Html.ValidationMessageFor(model =>model.Firewall.ConsoleServerID)
</div>
<div >
<span class="f">@Html.DisplayNameFor(model=>model.Firewall.ConsoleServerPort)</span>
@Html.TextBoxFor(model => model.Firewall.ConsoleServerPort)
@Html.ValidationMessageFor(model => model.Firewall.ConsoleServerPort) |
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
Edit After installing the foolproof i am facing the following problems :-
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
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.
if the user tried to submit the form where the ConsoleServerID is null then i will recevie the following exception :-
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
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
2 items) at System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry, IDictionary2 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
2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8
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.<>c__DisplayClass37.<>c__DisplayClass39.b__33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>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: