Reputation: 11
So I'm creating a website with MVC4. Essentially what I have is a table of licenses, with a bunch of properties. I want to be able to add checkboxes to this table so that when you select a group of licenses and click the submit button it will allow you to bulk edit a select few properties of the selected licenses (for example, the issued dates on them). I can't seem to find an easy way to implement this.
Upvotes: 0
Views: 1617
Reputation: 7692
I'm not sure if there is an easy way to do this. However, I'll propose one solution. The steps are:
So, let's dig into it.
Model
public class License
{
public int Id { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
}
1st action
public ActionResult Index()
{
List<License> viewModel = new List<License>();
viewModel.Add(new License() { Id = 1, Name = "Whatever" });
viewModel.Add(new License() { Id = 2, Name = "Whatever else" });
viewModel.Add(new License() { Id = 3, Name = "Nevermind this one" });
return View(viewModel);
}
View
@using (Html.BeginForm("Save", "Home"))
{
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Notes</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
@{
int i = 0;
foreach (MVC4.Test.Models.License l in Model)
{
<tr>
<td><input type="text" name="licenses[@i].Id" readonly value="@l.Id"/></td>
<td><input type="text" name="licenses[@i].Name" readonly value="@l.Name" class="_mayEdit" /></td>
<td><input type="text" name="licenses[@i].Notes" readonly value="@l.Notes" class="_mayEdit"/></td>
<td><input type="checkbox" class="_edit" /></td>
</tr>
i++;
}
}
</tbody>
</table>
<input type="submit" value="Save" />
}
<script type="text/javascript">
$(function () {
$('._edit').click(function () {
if ($(this).is(':checked'))
$(this).parent().parents('tr').find('._mayEdit').removeAttr('readonly');
else
$(this).parent().parents('tr').find('._mayEdit').attr('readonly', 'readonly');
});
});
</script>
2nd action
[HttpPost]
public ActionResult Save(License[] licenses)
{
return View();
}
A few things to note:
Upvotes: 1