0

更新:发现我认为 modelState.isValid 的原因是错误的。该模型有 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!");
        }
4

1 回答 1

0

我终于找到了遮阳篷。模型确实不一致。输入复选框未发送到模型,因此导致 model.isValid 为假。

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

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

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

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

非常感谢!

于 2012-11-21T01:11:44.483 回答