Reputation: 48716
I'm having an issue where my all the check boxes rendered in the view are coming out checked. I put a breakpoint at the line where my view model is constructed and through the debugger, I can see that some values are set to "true" and others are set to "false". So the problem, I'm assuming, has got to be in the view itself.
Here is my model:
public class UserModulesAdministrationViewModel
{
public bool AccessGranted { get; set; }
public int ModuleId { get; set; }
public string ModuleName { get; set; }
public string ModuleDescription { get; set; }
}
Here is my controller that is rendering the list:
public ActionResult UserModules(int id)
{
// Database stuff here
var model = modules.Select(module => new Infrastructure.ViewModels.UserModulesAdministrationViewModel
{
ModuleId = module.AccessModuleId,
AccessGranted = userModules.Any(z => z.AccessModuleId == module.AccessModuleId),
ModuleName = module.ModuleName,
ModuleDescription = module.ModuleDescription
}).ToList();
return View(model);
}
And finally here is my relevant view code:
@model IEnumerable<UserModulesAdministrationViewModel>
@foreach (UserModulesAdministrationViewModel m in Model)
{
<div class="col-md-4" style="margin-top: 15px;">
<div class="moduleBlockLong" style="position: relative">
<div class="moduleHeader">@m.ModuleName</div>
<div class="moduleText">@m.ModuleDescription</div>
<div class="checkbox" style="position: absolute; bottom: 0; right: 80px">
<label>
@{
var m1 = m;
}
@Html.CheckBoxFor(z => m1.AccessGranted )
<input type="checkbox" value="" checked="checked"/> Allow Access
</label>
</div>
</div>
</div>
}
Upvotes: 1
Views: 468
Reputation: 2629
I think that happens because you left <input type="checkbox" value="" checked="checked"/>
Remove it and it will works.
Also there exist another problem about foreach
loop.
ASP.NET MVC 4 - for loop posts model collection properties but foreach does not
Solution:
@for(var i = 0; i<Model.Count; i++)
{
<div class="col-md-4" style="margin-top: 15px;">
<div class="moduleBlockLong" style="position: relative">
<div class="moduleHeader">@Model[i].ModuleName</div>
<div class="moduleText">@Model[i].ModuleDescription</div>
<div class="checkbox" style="position: absolute; bottom: 0; right: 80px">
<label>
@Html.CheckBoxFor(z => Model[i].AccessGranted) Allow Access
</label>
</div>
</div>
</div>
}
Upvotes: 1
Reputation: 1
Try this code...
Instead of : <input type="checkbox" value="" checked="checked"/> Allow Access
Try:
<input type="checkbox" value="" checked="@m.AccessGranted "/> Allow Access
In addition, don't use m1 parameter..
Upvotes: 0
Reputation: 30597
In your view, remove
<input type="checkbox" value="" checked="checked"/> Allow Access
Because of checked="checked"
, this will always print out a checked checkbox.
Upvotes: 1
Reputation: 33306
The problem seems to me like you have hardcoded the input after the CheckBoxFor
HtmlHelper
.
@Html.CheckBoxFor(z => m1.AccessGranted )
<input type="checkbox" value="" checked="checked"/>
Remove:
<input type="checkbox" value="" checked="checked"/>
It's also worth noting that as you are using a foreach
loop rather than a for
loop that you will not be able to post the selected values back to the server.
You will need to index your loop as follows:
@for (var i = 0; i < Model.Count; i++)
{
// code
@Html.CheckBoxFor(z => Model[i].AccessGranted)
// rest of code
}
Or you will not be able to read any user input on the server.
Upvotes: 3