user8270367
user8270367

Reputation:

ASP.NET MVC Change Database value when checkbox is checked

I have a really simple question. I have a form where if you check the checkbox and submit the form it changes the value to true (it's false by default). At the moment it doesn't work for me. So I am asking how should I do it?

Here's a few things how I do them. There's a value "IsConfirmed"

public virtual bool IsConfirmed {get;set;}

And I have a simple HttpPost method.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit([Bind(Include = "UserName,Id,Email")] ApplicationUser formuser, string id, string RoleId)
    {
        var role = new ApplicationUser() { Id = formuser.Id, Email = formuser.Email, IsConfirmed = formuser.IsConfirmed };
        await UserManager.UpdateAsync(role);
        return RedirectToAction("Index");
    }

Here's my view

@model CPO.Models.ApplicationUser
@{
ViewBag.Title = "Edit";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.HiddenFor(model => model.Id)

<table class="table table-bordered table-hover">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
    </tr>
        <tr>
            <td>
                @Html.HiddenFor(model => model.Email)
                @Html.DisplayFor(model => model.Email)
               @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
            </td>
            <td>
                @Html.HiddenFor(model => model.UserName)
                @Html.DisplayFor(model => model.UserName)
                @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
            </td>
            <td>
                @Html.HiddenFor(model => model.FirstName)
                @Html.DisplayFor(model => model.FirstName)
            </td>
            <td>
                @Html.HiddenFor(model => model.LastName)
                @Html.DisplayFor(model => model.LastName)
            </td>
            <td>
                @Html.DropDownList("RoleId", "Select Role")
            </td>
            <td>
                @Html.EditorFor(model => model.IsConfirmed)
            </td>
            <td>
                <input type="submit" value="Edit" class="btn btn-default"/>
            </td>
        </tr>
</table>
}
@section Scripts {

@Scripts.Render("~/bundles/jqueryval")

}

Weirdly but it doesn't work and I have no idea why, maybe I missed to define something, but the model get's it's value as false even though it is checked.


Any help is highly appreciated, If I made mistakes please be kind to write I made them in the comments and I'll fix them

Upvotes: 2

Views: 1107

Answers (1)

user3559349
user3559349

Reputation:

You have excluded the IsConfirmed property from binding by your use of the BindAttribute

[Bind(Include = "UserName,Id,Email")]

which means only bind the values for properties UserName, Id and Email

Remove the attribute, or change it to include the property

[Bind(Include = "UserName, Id, Email, IsConfirmed")]

Note also you have excluded properties FirstName, LastName and RoleId from binding so there is little point including a form controls for them

Upvotes: 2

Related Questions