调用ajax.post时未实现验证类型

时间:2013-06-11 18:08:39

标签: c# jquery ajax asp.net-mvc model-binding

我想在将模型发送到我的Action之前验证一些字段。

这是我要验证的文本框

@Html.TextBoxFor(cModel => cModel.Value, new { id = "txtbLimit", @type = "int" })
@Html.ValidationMessageFor(cModel => cModel.Value)

和ajax帖子:

$.post('@Url.Action("AddUpdateConfigs")',
    {QueueMonitorConfigurationsID: ident, QueueMonitorConfigTypeName: $('#ddlConfigTypeName').val(), Threshold:$('#ddlThreshold').val(), QueueMonitorValueTypeName:$('#ddlValueTypeName').val(), Location: $('#txtbLocation').val(), Value: $('#txtbLimit').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');

它所调用的功能:

public JsonResult AddUpdateConfigs(StorageConfigurationModel modelbind)
{
    //Take the list of configurations
    IEnumerable<StorageConfigurationModel> configList = (IEnumerable<StorageConfigurationModel>)Session["ConfigurationList"];
    //Loop
    foreach (StorageConfigurationModel configModel in configList)
    {
        //Is it a duplicated entry?
        if ((configModel.QueueMonitorConfigTypeName == modelbind.QueueMonitorConfigTypeName) && (configModel.Location == modelbind.Location) && (configModel.QueueMonitorValueTypeName == modelbind.QueueMonitorValueTypeName) && (configModel.Threshold == modelbind.Threshold))
        {
            //Found duplicated entry
            return Json(new { Success = false, Description = "Duplicate entry" });
        }
    }
    //If not duplicated, add it to DB

    try
    {
        if (modelbind.Location.StartsWith("\\"))
        {
            DirectoryInfo dir = new DirectoryInfo(modelbind.Location);
            if (dir.Exists)
            {
                int finalValue = 0;
                int pathInt = 0;
                int valueTypeInt = 0;

                if (modelbind.QueueMonitorConfigTypeName == PathType.Path)
                    pathInt = 1;
                else
                    pathInt = 2;
                switch (modelbind.QueueMonitorValueTypeName)
                {
                    case UnitType.Percentage:
                        valueTypeInt = 1;
                        break;
                    case UnitType.MB:
                        valueTypeInt = 2;
                        break;
                    case UnitType.GB:
                        valueTypeInt = 3;
                        break;
                    case UnitType.TB:
                        valueTypeInt = 4;
                        break;
                    case UnitType.Files:
                        valueTypeInt = 5;
                        break;
                }

                if (modelbind.Threshold == ThresholdType.Upper)
                    finalValue = modelbind.Value;
                else
                    finalValue = Convert.ToInt32("-" + modelbind.Value);
                Boolean result = false;
                result = DAL.queryAddUpdateConfig(modelbind.QueueMonitorConfigurationsID, pathInt, modelbind.Location, finalValue, valueTypeInt);
                return Json(new { Success = result, Description = (result) ? ((modelbind.QueueMonitorConfigurationsID == -1) ? "New data inserted" : "Data updated") : "Error in Data types" });

            }
            else
            {
                return Json(new { Success = false, Description = "Location Path is not correct" });
            }
        }
        else
        {
            return Json(new { Success = false, Description = "Location Path has to be UNC path" });
        }
    }
    catch (Exception j)
    {
        return Json(new { Success = false, Description = "Error: " + j });
    }
}

聪明到足以使模型出现但不进行验证。 如果我在textbox中放置一个字符串,其中值(int)应该是,它将它转换为0并且不进行验证。

我还使用正则表达式验证了位置...再次无效。

有人看错了吗?谢谢

编辑:

我有:

  @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "form" }))
    {}

这是在ajax帖子之前:

$('#form').validate();

1 个答案:

答案 0 :(得分:0)

如果ModelState.IsValid,我看不到您的代码。另外你应该调用

$('form').validate();

在客户端验证您的表单,甚至使用Ajax.BeginForm