I'm doing forms on ASP .NET MVC 5. I thought it was simple enough to just post with a matching model in the action but I keep getting null values on my action. For example,
@model Tuple<ManageLoginsViewModel, IndexViewModel, LocalPasswordModel, SetPasswordViewModel>
<p>
You do not have a local password for this site. Add a local
password so you can log in without an external login.
</p>
@using (Html.BeginForm("SetPassword", "Manage", FormMethod.Post))
{
@Html.AntiForgeryToken()
<fieldset>
<legend>Set Password</legend>
<div>
<div class="LabelPassword">
<label>New password</label>
<input data-val="true" data-val-length="The New password must be at least 6 characters long."
data-val-length-max="30" data-val-length-min="6"
data-val-required="The New password field is required."
id="NewPassword" type="password">
</div>
<div class="LabelPassword">
<label>Confirm Password</label>
<input data-val="true" data-val-length="The New password must be at least 6 characters long."
data-val-length-max="30" data-val-length-min="6"
data-val-required="The New password field is required."
id="ConfirmPassword" type="password">
</div>
</div>
<input type="submit" value="Set password" />
</fieldset>
}
While numerous post suggest on renaming the action parameter to something that doesn't match the properties on my view model, I have not found a solution to this.
weirdly enough, there is no post data that Chrome shows. Is this an error in my html or my handling of the post?
and finally, here's the action method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SetPassword(SetPasswordViewModel setPassword)
{
if (ModelState.IsValid)
{
var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), setPassword.NewPassword);
if (result.Succeeded)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user != null)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
}
return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess });
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(setPassword);
}
I'm sure its something simple, but I've got no indication where things are going wrong now, other then the blanks on POST that is shown in Chrome (well, the anti-forgery token is displayed at least). Is this the problem?
Aucun commentaire:
Enregistrer un commentaire