mercredi 24 août 2016

ASP.NET MVC On form post, how to get the check status of a checkbox in the view?

In a system where I am using the Identity framework, I have the following form in a view that is bound to the model AppUser:

<div class="form-group">
    <label>Name</label>
    <p class="form-control-static">@Model.Id</p>
</div>
@using (Html.BeginForm("Edit", "Admin", new { returnUrl = Request.Url.AbsoluteUri }, FormMethod.Post, new { @id = "edituserform" }))
{

    @Html.HiddenFor(x => x.Id)
    <div class="form-group">
        <label>Email</label>
        @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label>Password</label>
        <input name="password" type="password" class="form-control" />
    </div>

 <input type="checkbox" name="userLockout" value="Account Locked" @(Html.Raw(Model.LockedOut ? "checked=\"checked\"" : "")) /> @:Account Locked <br>

<button type="submit" class="btn btn-primary">Save</button>
    <button class="btn btn-default"
            id="canceleditbutton">
        Cancel
    </button>
}

The model definition for AppUser:

public class AppUser : IdentityUser
{
   public bool LockedOut { get; set; }

   /*Other fields not shown for brevity*/

}

My specific question is regarding the checkbox for the LockedOut flag, which is a custom property I added. For a test user, I manually set the flag in the database to True and as expected, on the view the checkbox was checked when it was loaded. Now my goal is to be able to access this in the POST edit method of the AdminController that this form calls on submit. The skeleton for that method is as follows:

[HttpPost]
public async Task<ActionResult> Edit(string id, string email, string password, string userLockout)
{
 //Code here to change the LockedOut value in the database based on the input received
}

The issue is that the userLockout parameter comes in as null when I click Save on the submit on the edit screen. The other two values are populated correctly. How can I access the userLockout value, so that I can continue with saving the change into the database if needed?

And lastly, my ultimate goal here is to implement a system where an admin can lock or unlock a user account via the LockedOut flag (which gets checked each time someone logs in). I know the Identity framework has support for lockouts, but this seems to be time restricted lockouts only. Is there a way that exists in the Identity framework to have permanent (unless manually changed) lockouts? I am trying to use as little custom code and design as possible, so that's why I am interested in knowing this as well. Particularly, I am interested in using the way the framework keeps track of unsuccessful login attempt counts, because I want to try to avoid implementing that manually as well.

Thank you.




Aucun commentaire:

Enregistrer un commentaire