ModelState.IsValid不排除必需的属性

时间:2010-07-02 14:26:19

标签: asp.net-mvc

我试图排除必需的属性(密码),因此模型状态不会验证该属性,但由于某种原因,即使我尝试排除它,它仍然会验证。

控制器:

    [Authorize, AcceptVerbs(HttpVerbs.Post)]
    public ActionResult _Edit(int id, [Bind(Exclude = "Password")]FormCollection collection)
    {
        var user = Proxy.GetUser(id);

        TryUpdateModel(user, null, null, new[]{"Password"});

        if(!ModelState.IsValid)
            return PartialView(user);

        Proxy.UpdateUser(user);
    }

查看:

   ...
   <tr>
       <td class="label">
           <label class="row_description" for="Password"><%= S._("Password")%></label>
       </td>
       <td>
           <%= Html.Password("Password", null, new { @class = "row_input" })%>
           <%= Html.ValidationMessage("Password", "*")%>
       </td>
   </tr>

用户(使用dataannotation):

[Required]
public string Password { get; set; }

我正在使用VS2008,MVC2,firefox

也许我只是累了,看不到它。任何帮助表示赞赏

7 个答案:

答案 0 :(得分:16)

我目前遇到与MVC3类似的问题。

尽管我的操作中[Bind(Exclude = "Password")]ModelState.IsValid仍然返回false。

我注意到TryUpdateModel(user, null, null, new string[]{"Password"});成功更新了模型;但仍然返回假。然后我发现(在stackoverflow的某处,为没有链接道歉)TryUpdateModel实际返回ModelState.IsValid

因此,问题不在于TryUpdateModel,而在于ModelState.IsValid

注意:这也意味着您无需再验证两次......您可以使用此代码:

if (!TryUpdateModel(user, null, null, new string[]{"Password"}))
    return PartialView(user);

因此,问题似乎是ModelState仍在验证已从FormCollection中排除的属性。

我能够通过在调用ModelState之前从TryUpdateModel删除字段来解决此问题:

ModelState.Remove("Password");

请注意,TryUpdateModel仍然需要根据上述代码从更新中排除属性列表。

答案 1 :(得分:3)

我在ASP .NET MVC 2中使用以下方法取得了成功

TryUpdateModel(user);
ModelState.Remove("Password");
if (!ModelState.IsValid) return PartialView(user);

为了防止TryUpdate绑定到某些模型属性,您可以创建一个包含模板,如下所示:

public interface IUserValidateBindable
{
    string UserId { get; set; }
}

public class User : IUserValidateBindable
{
    [Required]
    public string UserId { get; set; }
    [Required]
    public string Password { get; set; }
}

更新TryUpodateModel调用如下:

TryUpdateModel<IUserValidateBindable>(user);

答案 2 :(得分:1)

您可以使用类似的扩展方法:

public static bool IsValidExclude(this ModelStateDictionary modelState, params string[] exclude)
{
    foreach (var key in exclude)
    {
        if (modelState.ContainsKey(key))
            modelState.Remove(key);
    }

    return modelState.All(m => m.Value.Errors.Count == 0);
}

然后打电话:

var result = ModelState.IsValidExclude("Password");

答案 3 :(得分:0)

也许你应该替换

TryUpdateModel(user, null, null, new[]{"Password"});

TryUpdateModel(user, null, null, new string[] {"Password"});

因为TryUpdateModel使用哪个重载可能会让人感到困惑。 只是说...

答案 4 :(得分:0)

我成功使用了[Bind(Exclude = "Property")]ModelState.Remove("Property"),它就像魅力一样。

答案 5 :(得分:0)

在我的情况下,我在modelstate.isValid上遇到了错误,因为Remove在webapi asp.net mvc中对我不起作用,所以我跟踪错误

enter image description here

然后我添加将模型定义为参数的模型ModelState.Remove("model.Description");,然后使用参数名称,然后modelstate对我起作用,如下所示:

enter image description here

答案 6 :(得分:-2)

好像我回答太迟了,但我也遇到了同样的问题。

检查您的ModelState.Keys集合。密钥可能采用modelObjectName.Password的形式,其余的模型属性也是如此。

所以在这种情况下ModelState.Remove("Password")将不起作用。您应该尝试ModelState.Remove("modelObjectName.Password")

希望这能解决某人的问题:)

相关问题