有没有办法在客户端添加动态验证?

时间:2017-06-29 10:33:48

标签: asp.net asp.net-mvc validation

我正在使用C#和.NET Framework 4.7开发ASP.NET MVC。

我在视图上有这个: enter image description here

右边的值将具有最大长度,具体取决于我们在左侧的选择中选择的内容。

这是视图代码:

import * as types from './types';

export default function incrementCounter(counterValue) {
  return {
    type: types.INCREMENT_COUNTER,
    counterValue,
  };
}

模型类:

for (int vDataIndex = 0; vDataIndex < Model.VariableDataList[levelIndex].VariableDataForLevel.Count; vDataIndex++)
{
    <tr>
        <td>
            <div class="group">
                @Html.HiddenFor(m => m.VariableDataList[levelIndex].VariableDataForLevel[vDataIndex].ProductionOrderId)
                @Html.HiddenFor(m => m.VariableDataList[levelIndex].VariableDataForLevel[vDataIndex].AggregationLevelConfigurationId)
                @Html.DropDownListFor(
                    m => m.VariableDataList[levelIndex].VariableDataForLevel[vDataIndex].VariableDataId,
                    new SelectList(Model.variableDataItems, "Id", "Name",
                    Model.VariableDataList[levelIndex].VariableDataForLevel[vDataIndex].VariableDataId))
            </div>
        </td>
        <td>
            <div class="group">
                @Html.TextBoxFor(
                    m => m.VariableDataList[levelIndex].VariableDataForLevel[vDataIndex].Value)
                @Html.ValidationMessageFor(m => m.VariableDataList[levelIndex].VariableDataForLevel[vDataIndex].Value)
            </div>
        </td>
    </tr>
}

也许我可以在[{1}}中存储长度,并使用它创建动态验证,具体取决于所选的public class VariableDataGrouped { public string LevelName { get; set; } public List<Models.VariableData> VariableDataForLevel { get; set; } } public class CreateVariableDataViewModel { public readonly List<GenericIdNameType> variableDataItems; [ ... ] public List<VariableDataGrouped> VariableDataList { get; set; } public CreateVariableDataViewModel() { variableDataItems = new List<GenericIdNameType>(); foreach (VariableDataKey key in VariableDataKey.GetAllVariableData()) { GenericIdNameType gType = new GenericIdNameType() { Id = key.Id, Name = key.Description }; variableDataItems.Add(gType); } } } public class Models.VariableData { [ ... ] [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "VariableDataValueRequired")] [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "VariableDataValueLong")] public string Value { get; set; } }

您知道如何根据所选的CreateVariableDataViewModel.variableDataItems创建动态验证吗?

0 个答案:

没有答案