ModelState.IsValid无效。为什么我可以创建用户?

时间:2012-11-20 17:22:23

标签: asp.net-mvc asp.net-mvc-4 model

更新:找到我认为是modelState.isValid的原因是false。 该模型有7个属性...并且我发现我发现该模型只从视图中接收了6个属性。

我的复选框未发送给控制器?我是否应该做些额外的配置来实现它?

这是观点:

 @using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "closeDialog('RegistroUsuario', '<b>Usuário cadastrado com Sucesso!</b>', true)" }, new { id = "FormRegistroUsuario" }))
{
   <fieldset>
        <legend>Cadastro novo Usuário</legend>
       <table id="changePassword">
                <tr>
                    <td class="smallField">Username:</td>
                    <td>@Html.TextBoxFor(m => m.UserName,new { @class = "validate[required]" }) @Html.ValidationMessage("usernameVal", "*")</td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td>@Html.PasswordFor(m => m.Password, new { @class = "validate[required]" }) @Html.ValidationMessage("passwordVal", "*")</td>
                </tr>
                <tr>
                    <td>Repetir Senha:</td>
                    <td>@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "validate[required,equals[Password]]" }) @Html.ValidationMessage("senhaVal", "*")</td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td>@Html.TextBoxFor(m => m.Email, new { @class = "validate[required,custom[email]]" }) @Html.ValidationMessage("emailVal", "*")</td>
                </tr>
                <tr>
                    <td>Pergunta Secreta:</td>
                    <td>@Html.TextBoxFor(m => m.SecretQuestion, new { @class = "validate[required]" }) @Html.ValidationMessage("secretVal", "*")</td>
                </tr>
                               <tr>
                    <td>Resposta:</td>
                    <td>@Html.TextBoxFor(m => m.SecretQuestionPassword, new { @class = "validate[required]" }) @Html.ValidationMessage("awnserVal", "*")</td>
                </tr>
                <tr>
                    <td>Ativo:</td>
                    <td><input type="checkbox" name="status" id="status" value="Ativo" data-val="true"/> @Html.ValidationMessage("activeVal", "*")</td>
                </tr>    
            </table>  
       <input type="submit" value="Criar Usuário" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button-link"/>         
    </fieldset>

}

感谢...


这是一种非常奇怪的行为 我仍然因某种原因而创建了用户。

有些时候,由于已有用户名,因此无法创建用户。 (即使用户不存在)..非常奇怪

 if (ModelState.IsValid)
        {

            MembershipProvider mp = Membership.Provider;
            MembershipCreateStatus Status;
            string erroMsg;

            if (String.IsNullOrEmpty(Request.Form["status"]))
            {
                model.Active = false;
            }
            else
            {
                model.Active = true;
            }

            MembershipUser newUser = mp.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretQuestionPassword, model.Active, Guid.NewGuid(), out Status);

            try
            {

                if (newUser == null)
                {
                    erroMsg = GetErrorMessage(Status);
                    return Content(erroMsg);
                }
                else
                {
                    return Content("Usuário Cadastrado");
                }

            }
            catch (MembershipCreateUserException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new MembershipCreateUserException("An exception occurred creating the user.", e);
            }
        }
        else
        {
            return Content("isValid is false!");
        }

1 个答案:

答案 0 :(得分:0)

我终于找到了芒果。 该模型确实不一致。输入复选框未发送到模型,因此导致model.isValid为false。

我使用helper html复选框解决了这个问题。

<td>@Html.CheckBoxFor(m => m.Active)</td>

我所要做的就是获取正确的复选框值

bool checkvalue = bool.Parse(Request.Form.GetValues("Active")[0]);

非常感谢!