-1

I am using Identity 2.0 and in my ApplicationUser I have the following:

ApplicationUser.cs

    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        //other things omitted for brevity
    }

In my view I have the following simplified version:

@model ApplicationUser
@using (Html.BeginForm("Details", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<dl class="dl-horizontal ">
    <dt>
        @Html.DisplayNameFor(model => model.FirstName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FirstName)

    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.LastName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.LastName)
    </dd>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
}

My controller

        [HttpPost]
        public async Task<ActionResult> Details (ApplicationUser model)
        {
            //do checks on which items have been selected
            return View();
        }

I want to add checkbox next to each of the labels, and in my POST action I want to be able to see which have been selected. Eg. FirstName = checked, LastName = notChecked or anything similar. Which approach should I take? I have tried checkboxfor, but without any success.

Thunderer
  • 159
  • 3
  • 17
  • This is why we use [View Models](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) – Jasen Mar 23 '16 at 21:39
  • What are you trying to achieve? What is the point of the checkboxes? If you want some checkboxes, add properties `bool IsFirstNameChecked` etc and add `@Html.CheckBoxFor(m => m.IsFirstNameChecked)` in your view. –  Mar 23 '16 at 21:39
  • I will use the selected items to send email to users which fields were not populated the right way. First name and Last name are here for simplicity reasons given. – Thunderer Mar 23 '16 at 22:54
  • @Jasen, thats correct and I am aware of the view models as I use it in other places. BUT here specifically I want to be able to reuse ApplicationUser class, without having to manually add properties that are already there. Say 20 properties, each would need a bool variable. Cumbersome to maintain. Is there some other way to wrap it without using zillion bool variables? – Thunderer Mar 23 '16 at 23:02
  • The form post binds by name. Since you don't want to add any properties to any models just [add two input boxes for each checkbox](http://stackoverflow.com/questions/14730746/getting-checkbox-value-in-asp-net-mvc-4). Then your action will include the additional parameters `Details(ApplicationUser model, bool firstNameChecked, bool lastNameChecked)`. – Jasen Mar 23 '16 at 23:54
  • @Thunderer, Why do you think you need checkboxes for this. In the POST method, you can check which fields have a value. And if the fields are required, why are you not adding the `[Required]` attribute? –  Mar 23 '16 at 23:59
  • all fields have a value as it is populated in the step before. Its not simply checking if something is there or not, its to visually inspect. e.g you uploaded wrong pdf document, i check mark it, and you get email that you uploaded wrong. – Thunderer Mar 24 '16 at 00:33

1 Answers1

1

I think it's better to use a ViewModel:

public class UserViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool UseFirstName { get; set; }
    public bool UseLastName { get; set; }
}

Then map the IdentityUser to UserViewModel in get method and in the view:

@model UserViewModel
@using (Html.BeginForm("Details", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{  
<dl class="dl-horizontal ">
 <dt>
     @Html.DisplayNameFor(model => model.FirstName)
 </dt>

 <dd>
 @Html.CheckBoxFor(model => model.UseFirstName)
 @Html.DisplayFor(model => model.FirstName)

</dd>
  • One way to put it, but that means then I have to updated my ApplicationUser and this, and possibly other, viewModels. Given that I have 20 properties it becomes cumbersome to maintain, as I would have 40 properties in your implementation of the VM. – Thunderer Mar 23 '16 at 22:58