How to check the properties of non-model?

In my application, I dynamically create a user interface based on runtime data. There is a model there, but it does not have compiled properties. Instead, I check the database and render several fields using these helpers:

@Html.TextBox("name", RunTimeValue)

Now I would like to include validation in these fields, but I don’t see how to do it. The custom MetadataProvider does not seem to work, as it still expects the model to have properties, while the provider provides the attributes. But my model properties just don't exist until runtime. For this reason, I also can not be used, for example EditorFor. So, how can I introduce validation (client and server) in this situation?

+3
source share
5 answers

If you have unobtrusive check enabled, you can cheat by doing this

@Html.TextBox("name", RunTimeValue, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "The name is required" } })

Other possible data-val values ​​from the window are data-val-regex (with data-val-regex-pattern parameter), data-val-range (with data-val-range-min and data-val-range-max range) , data-val-number and some others that I have not used.

+5
source

you can use client-side validation using the jquery validation method, for example:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
      $(document).ready(function () {
          $("form").validate({
              rules: {
                  name: "required"
              }
          });
      }); 
 </script>
0
source

?

db, max length , null , ..? db build, . , . , +, , , .

? , , , .

0

, jQuery.validate .

, , , , - . MVC-ish.

: , , IEnumerable, MyFields:

public class MyFields {
    public string Name { get; set; }
    public string Value { get; set; }
    public string ErrorMessage { get; set; }
    // TODO: Add fields for validation expectations: required, string length, etc
    // TODO: Maybe consider adding fields to specify the control needed: checkbox, select, etc
}

, - , .

0
  • , .
  • , , ,

    @Html.TextBox("name", RunTimeValue, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "The name is required" } })

  • @Html.ValidationMessage("name", "", new { @class = "text-danger" })

  • JQuery

    @section Scripts {@Scripts.Render("~/bundles/jqueryval")}

0

All Articles