我有这个:
@Html.TextBoxFor(cModel => cModel.Value, new { id = "txtbLimit", @type = "int" })
我想确保用户输入的内容是一个整数。
我怎样才能做到这一点?
编辑:我不使用模型的值来解析这个文本框,所以模型验证不是我想要的
编辑2:
模型:
public class StorageConfigurationModel
{
[Required]
public int QueueMonitorConfigurationsID { get; set; }
[Required]
public PathType QueueMonitorConfigTypeName { get; set; }
[Required]
public string Location { get; set; }
[Required]
public UnitType QueueMonitorValueTypeName { get; set; }
[Required]
public ThresholdType Threshold { get; set; }
[Required]
[RegularExpression(@"\d*")]
public int Value { get; set; }
}
public enum PathType
{
Path
}
public enum UnitType
{
MB, GB, TB, Files, Percentage
}
public enum ThresholdType
{
Upper, Lower
}
解析函数:
private static StorageConfigurationModel BindToModel(int id, string pathType, string threshold, string valueType, string location, int limit)
{
return new StorageConfigurationModel
{
QueueMonitorConfigurationsID = id,
QueueMonitorConfigTypeName = (PathType)Enum.Parse(typeof(PathType), pathType),
Location = Convert.ToString(location.Trim()),
Value = Convert.ToInt32(limit),
QueueMonitorValueTypeName = (UnitType)Enum.Parse(typeof(UnitType), valueType),
Threshold = (ThresholdType)Enum.Parse(typeof(ThresholdType), threshold)
};
}
因此,当我将所有数据放在视图中并单击添加时,什么都不会触发。
有了这个,我调用了调用模型绑定器的函数:
$.post('@Url.Action("AddUpdateConfigs")',
{id: @Model.QueueMonitorConfigurationsID , pathType: $('#ddlConfigTypeName').val(), threshold:$('#ddlThreshold').val(), valueType:$('#ddlValueTypeName').val(), location: $('#txtbLocation').val(), limit: $('#txtbLimit').val(), config: $('#NewOrUpdate').val() },
function(data){
if (!data.Success){
alert(data.Description);
}
else{
//$('#gridView').load('/Storage/gvConfigurations');
$.get('@Url.Action("gvConfigurations", "Storage")',null,function(data){$('#gridView').html(data);},'html');
}
},'json');