Reputation: 186
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.
Upvotes: 0
Views: 165
Reputation: 1038710
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:
jquery
jquery.validate
jquery.validate.unobtrusive
Upvotes: 2