2

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

13
  • 1
    When you debug, where exactly is the value lost? What is included in the POST request? What is in the model object received by the controller action? What is in the result of model.ToDomainModel()? What is in the commission object? Commented Sep 26, 2017 at 13:08
  • Can you show your complete razor code also? Commented Sep 26, 2017 at 13:10
  • The value of model.IsConfirmed will be true if the checkbox is checked based on the code you have shown. Are you referring to the value of commission.IsConfirmed? Commented Sep 26, 2017 at 13:10
  • that's what I'm saying. it should be true but it gets false. Commented Sep 26, 2017 at 13:11
  • I added the ToDomainModel. And Sorry David, but that is too many questions to answer, basically you want all the code, but I can tell you the problem isn't where you're asking. Commented Sep 26, 2017 at 13:11

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, you were very helpful !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.