Skip to content

Instantly share code, notes, and snippets.

@dgg
dgg / gist:6387157
Created August 30, 2013 07:28
Model binding dynamic collections. II Collection display in a loop
<tbody>
@for (int i = 0; i < Model.Things.Length; i++)
{
<tr>
<td>
@Html.DisplayFor(m => Model.Things[i].Number)
</td>
<td>
@Html.DisplayFor(m => Model.Things[i].Text)
</td>
@dgg
dgg / gist:6387167
Created August 30, 2013 07:30
Model binding dynamic collections. II Collection display using template
<tbody>
@Html.DisplayFor(m => m.Things)
</tbody>
@dgg
dgg / gist:6387180
Created August 30, 2013 07:31
Model binding dynamic collections. II HTML generated for collection
<input id="Things_1__Text" name="Things[1].Text" type="hidden" value="Two" />
@dgg
dgg / gist:6387222
Created August 30, 2013 07:38
Model binding dynamic collections. III IClientSortable
public interface IClientSortable
{
int ClientOrder { get; set; }
}
public class SortableThing : IClientSortable
{
public string Text { get; set; }
public int Number { get; set; }
public bool IsEven { get; private set; }
public int ClientOrder { get; set; }
@dgg
dgg / gist:6387243
Created August 30, 2013 07:42
Model binding dynamic collections. III View code
@using (Html.BeginForm("Index", "Sort", FormMethod.Post, new{role = "form", @class="form-inline"}))
{
<h1>Sorting</h1>
<table id="table-sortable" class="table table-bordered table-striped table-hover table-responsive">
<caption>Sortable things</caption>
<thead>. . .</thead>
<tbody>@Html.DisplayFor(m => m.Things)</tbody>
</table>
<div class="form-group">
<input type="submit" name="discard" value="Discard" class="btn btn-default" />
@dgg
dgg / gist:6387250
Created August 30, 2013 07:43
Model binding dynamic collections. III DisplayTemplate
<tr>
<td>
@Html.HiddenFor(m => m.ClientOrder, new { @class = "data-sort" })
@Html.HiddenFor(m => m.Number)
@Html.DisplayFor(m => m.Number)
</td>
<td>
@Html.HiddenFor(m => m.Text)
@Html.DisplayFor(m => m.Text)
</td>
@dgg
dgg / gist:6387271
Created August 30, 2013 07:46
Model binding dynamic collections. III sortable
DGON_DOTNET.ClientInteraction =
{
makeSortable: function (tableSelector, options) {
options = optionsWithDefaults(options, {
handleSelector: '.handle',
sortSelector: '.data-sort'
});
var tbody = $(tableSelector + ' tbody');
$(tbody).sortable({
helper: maintainWidth,
@dgg
dgg / gist:6387282
Created August 30, 2013 07:47
Model binding dynamic collections. III Client re-order
function reorder(rows, sortSelector) {
$(rows).each(function (index) {
var $row = $(this);
$row.find(sortSelector).val(index);
});
}
@dgg
dgg / gist:6387288
Created August 30, 2013 07:48
Model binding dynamic collections. III Controller
[HttpPost]
public ActionResult Index(string save, string discard, SortViewModel posted)
{
if (isSave(save, discard))
{
_repository.Save(SortableThing.ToThings(posted.Things));
}
IEnumerable<Thing> updatedThings = _repository.Find();
var model = new SortViewModel
{
@dgg
dgg / gist:6387292
Created August 30, 2013 07:49
Model binding dynamic collections. III Server-side sort
public static IEnumerable<Thing> ToThings(SortableThing[] from)
{
return from
.Sort()
.Select(st => st.ToThing());
}