1

Installed EA for trying to have a parameter be required if there is nothing in the database, IE. this is the first time someone is creating something.

However, the RequiredIf never fires for client side validation, even though when the model gets into the partial view, the BindingExists bool is set to false and the Xml value is still null.

Model:

    public class AddTestStepXmlParameterModel
    {

        public ParameterTypeEnum ParameterType { get; set; }

        public string ParameterName { get; set; }

        public string Description { get; set; }


        [RequiredIf("BindingExists == false", ErrorMessage = "An XML File is required: Please Try again")]
        [FileExtensions(Extensions = "xml", ErrorMessage = "Specify an XML file.")]
        public HttpPostedFileBase XmlValue { get; set; }

        public bool BindingExists { get; set; }

    }

Global.asax:

protected void Application_Start()
{                                                                                    
    ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.FirstOrDefault(x => x is DataAnnotationsModelValidatorProvider));
    ModelValidatorProviders.Providers.Add(new ExpressiveAnnotationsModelValidatorProvider());
}

Scripts in View:

<script src="~/Scripts/jquery-3.1.0.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/expressive.annotations.validate.js"></script>

Partial View Call:

 @Html.Partial("AddParameters", Model.AddTestStepModel.AddTestStepParametersModel)

Partial View:

@Html.HiddenFor(m => m.AddTestStepXmlParameterModels[k].BindingExists, new {@Value = Model.AddTestStepXmlParameterModels[k].BindingExists})
@Html.TextBoxFor(m => m.AddTestStepXmlParameterModels[k].XmlValue, new {type = "file", @class = "btn btn-default btn-file", style = "color:transparent", onchange = "this.style.color = 'black'"})
@Html.ValidationMessageFor(m => m.AddTestStepXmlParameterModels[k].XmlValue)

When using just a normal "Required", the Xml Value client side fires off fine, however using the RequiredIf fails to do any validation. I've followed the isntallation steps with the Global.asax

TLBB
  • 89
  • 1
  • 10

1 Answers1

0

Your HTML is not being generated as expected. Due to that, serialized form isn't properly understood by the model binder and cannot be correctly deserialized.

Instead of partial view use the editor template:

  • move the AddParameters.cshtml template under Views...\EditorTemplates\ directory,

  • change @Html.Partial(... invocation into @Html.EditorFor(model => model.AddTestStepModel.AddTestStepParametersModel, "AddParameters").

When you compare the output HTML for these two invocations you'll see

  • short input fields names for the partial view: AddTestStepXmlParameterModels[0].XmlValue,
  • in contrast to editor template rendering full names, used by the binder to map respective fields: AddTestStepModel.AddTestStepParametersModel.AddTestStepXmlParameterModels[0].XmlValue.
jwaliszko
  • 16,942
  • 22
  • 92
  • 158
  • I have confirmed that the RequiredIf validation now works for some properties of the model (I created a test string and used the same logic as the Xml parameter and it works) However, when trying to use it on the HttpPostedFileBase, the RequitedIf fails to check for the requirement and allows the user to submit the form without uploading a file. – TLBB Aug 29 '16 at 14:14