1

I have a product database, where people can add specifications to a product. Currently this looks like this: Application Screenshot

All these different specifications need custom validation so users can only enter certain value's for certain specifications. Currently it's defined in the database which specification is which datatype. Every specification has a foreign key to a datatype. I think i need to check which attribute is currently selected when submitting the form and have some sort of dynamic validation for the value field. But i can't find any similair situations already posted here.

I posted my current code, which is more of an outline of how i think it should work.

ValidationAttribute:

public class DropdownBasedValueValidationAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        AddSpecificationViewModel viewModelUsed = (AddSpecificationViewModel)validationContext.ObjectInstance;

        switch(viewModelUsed.datatypeSelected)
        {
            case "Int32":
                return checkInt32((int)value);
            case "Boolean":
                return checkBool((bool)value);
            case "String":
                return checkString(value.ToString());
        }
        return new ValidationResult("Entered value is incorrect");
    }
}

ViewModel:

[Display(Name = "Value")]
    [Required]
    [DropdownBasedValueValidation()]
    public string Value { get; set; }

View:

@using (Html.BeginForm("AddSpecification", "Products", FormMethod.Post))
                {
                    @Html.HiddenFor(m => m.productIdCode)

                    <div class="col-md-4">
                        <div class="form-group">
                            @Html.LabelFor(model => model.labelDropwdownValue)
                            @Html.DropDownList("LabelId", null, "All", new { @class = "form-control" })
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="form-group">
                            @Html.ValidationMessageFor(m => m.Value)
                            @Html.LabelFor(m => m.Value)
                            @Html.TextBoxFor(m => m.Value, null, new { @class = "form-control" })
                        </div>
                    </div>

                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary btn-block">Add Specification</button>
                    </div>
                }

I'm pretty new to MVC so i hope you guys can help me out here :)

Timon
  • 23
  • 3
  • you have to also write some extenstion to jquery validate so it covers custom validation logic – Babak Fakhriloo Nov 08 '17 at 12:10
  • Take a look at https://stackoverflow.com/questions/19726404/client-side-validation-in-custom-validation-attribute-asp-net-mvc-4 – Babak Fakhriloo Nov 08 '17 at 12:11
  • Does `Value` need to be either an `int`, or `bool` or `string` based on the value of the dropdownlist? –  Nov 08 '17 at 12:12
  • I'll take a look at that Babak, and yes stephen, like if you choose productcode from the dropdown, which is an int, than you should not be allowed to enter a string. – Timon Nov 08 '17 at 12:16
  • Everything is a string in the client :). While you could write a custom `ValidationAttribute` that implements `IClientValidatable` - refer [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](https://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) - I suggest this might be easier to just use a view model with 3 properties for `int` `bool` and `string`, and generate a control for each, and show/hide them based on the dropdownlist value (and apply a [foolproof](http://foolproof.codeplex.com/) `[RequiredIf]` attribute to each –  Nov 08 '17 at 12:19
  • @StephenMuecke Nice, that article is waaay more in depth than the one listed on microsoft's website! – Timon Nov 08 '17 at 12:48

0 Answers0