更新: 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!");
}