0

I am working on a MVC 3 app that requires at some point some data from the users. The data is then sent to an external DLL (I am not allowed to change it). The transfer is done by passing an object to a function so that object can be my Model for the View.

The problem is that I do not have access to add Attributes to the Model and I do not what to add a metadata class.

I decided to use a custom validation mechanism using JQuery so I made helper functions like this:

@helper TextBox(string name, object value = null)
    {
        var val = value ?? "";
    <tr>
        <td class="label">@name :
        </td>
        <td>
            <input type="text" name="@name" id="@name" value="@val" />
        </td>
    </tr>
}

@helper ValidationMessage(string name)
    {<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="@name"></span>}

@helper TextBoxRequired(string text, string fildName, object value = null, string message = "*")
    {
        var val = value ?? "";
    <tr>
        <td class="label">@text :</td>
        <td>
            <input type="text" name="@fildName" id="@fildName" value="@val" data-val="true" data-val-required="@message" />
            @ValidationMessage(fildName)</td>
    </tr>
}

I've included jquery-1.8.2, jquery.validate and jquery.validate.unobtrusive but it is not working.

Any help is appreciated.

Edit: web config settings:

  <appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

And I am using Firefox,Chrome and IE for testing with JavaScript enable.

2
  • Did you enable validation in web.config (set ClientValidationEnabled and UnobtrusiveJavaScriptEnabled under AppSettings to true), or via code using the same keynames as static properties on HtmlHelper? Commented Oct 23, 2012 at 19:48
  • yes. Sorry for not adding that too: <appSettings> <add key="webpages:Version" value="1.0.0.0" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> Commented Oct 23, 2012 at 19:49

1 Answer 1

2

I'd recommend you to use view models. Leave the domain models that you are not allowed to change in their assembly. Then have your controllers take/pass view models from/to the views. Those view models are classes that you specifically define for the requirements of a given view. The view models properties could then be decorated with whatever validation data annotations you need.

Then use standard and strongly typed helpers such as Html.TextBoxFor and Html.ValidationMessageFor in your view and don't reinvent wheels in some helpers.

By the way for client side validation to work you need at least the following 3 references in that order:

  1. jquery
  2. jquery.validate
  3. jquery.validate.unobtrusive
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.