Click here to Skip to main content
15,034,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to create a page where users enter some numeric fields, which are then processed by an external dll on the server. Calculation requires client-side validation, such as on intervals (calculation takes a long time, because it's very complex). These intervals are saved on the database and they can be varied over time, eg by extending some allowable range. In addition, these fields must be preloaded with default values, and some should not be visible to some users, depending on the role.
Given the variability of the intervals, I excluded to use the Range attribute in the model (because Range must be variable on the time, without need of recompile), So I created a custom helper that loads all needed data from database instead . The problem is that I'm not able to use ValidationHelper, as indicated in these articles!

What I have tried:

if I use
Validation.Add("field1", Validator.Range((double)min, (double)max, "some error"));
I get
Error (CS0117) - Validation does not contain a definition for 'RequireField'

if I use
ValidationHelper.Add("field1", Validator.Range((double)min, (double)max, "some error"));
I get
Error (CS0120): An object reference is required for the method 'ValidationHelper.Add(string, params IValidator[])'

if I try
ValidationHelper v = new ValidationHelper();
I get
Error CS1729: 'ValidationHelper' does not contain a constructor that takes 0 arguments

I tried to put something in it, but IntelliSense doesn't help me and in MSDN site I can't find how to initialize itValidationHelper Class (System.Web.WebPages)

min and max are correctly loaded from database (they are float)
Everything else, apart for Validation, works really fine (default value, hidden fields for unexperienced users and so on)

the field is
@html.TextBox("field1", default_value, new { @class = fieldclass })
@html.Hidden("field1", default_value);

to let it work the first lines of my custom Helper are
@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Web.WebPages;
@using System.Web;
@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Web.Mvc.Routing;
@using System.Web.Mvc.Razor;
@using System.Data.Entity;
@using mywebsite.Controllers;
@using mywebsite.Models;

@functions {
    private static WebViewPage page
        get { return PageContext.Page as WebViewPage; }

    private static System.Web.Mvc.HtmlHelper<dynamic> html
        get { return page.Html; }
    private static UrlHelper url
        get { return page.Url; }
    private static dynamic viewbag
        get { return page.ViewBag; }

After solving this big problem I will create a model to better manage all data: I will put something as
m => m.field1

I'm using Visual Studio 2015, these are my packages:
  <package id="Antlr" version="" targetFramework="net46" />
  <package id="bootstrap" version="3.3.7" targetFramework="net46" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net46" />
  <package id="" version="6.1.3" targetFramework="net46" />
  <package id="jQuery" version="3.1.1" targetFramework="net46" />
  <package id="jQuery.Validation" version="1.16.0" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" />
  <package id="" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" />
  <package id="" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.3" targetFramework="net46" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.Net.Compilers" version="1.3.2" targetFramework="net46" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Web.Infrastructure" version="" targetFramework="net46" />
  <package id="Modernizr" version="2.8.3" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46" />
  <package id="Owin" version="1.0" targetFramework="net46" />
  <package id="Respond" version="1.4.2" targetFramework="net46" />
  <package id="WebGrease" version="1.6.0" targetFramework="net46" />

and these are in my web.config (I changed they to be minimum of version
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="***" />
<bindingRedirect oldVersion="" newVersion="" />

<assemblyIdentity name="System.Web.Mvc" publicKeyToken="***" />
<bindingRedirect oldVersion="" newVersion="" />

<assemblyIdentity name="System.Web.WebPages" publicKeyToken="***" />
<bindingRedirect oldVersion="" newVersion="" />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900