Predicates

by MikeHogg 25. August 2012 12:18

 

 

LINQ is fantastically powerful. It has changed the way I approach almost every problem now in .net, from loosely typed collections like DataTables, and DataRows (only need these for interfaces now), to strong typed classes and IEnumerables, for dynamic filtering and creation of new collections with Lambdas.  Very powerful stuff.

But still I have found myself, a few times, passing a collection to a long list of methods, where in each one I pull a subset of objects that match a certain filter, over and over with each method doing just about the same thing, except for a different filter on the collection.  This time, I wanted to get closer to implementing the dynamic predicate (I think new in 4.0?), in a baby step, by passing the predicate as an argument. 

So where I initially did something like this:

 

 

 

IEnumerable<ACME_WEB.Models.FooEvent> events = ACME_WEB.lib.Repository.GetFooEvents();
 
            List<ACME_WEB.Models.FooEvent> _emailedevents = new List<ACME_WEB.Models.FooEvent>();
            _emailedevents.AddRange(GetFeeReminders(events));
            _emailedevents.AddRange(GetFighReminders(events));
            _emailedevents.AddRange(GetPhoReminders(events));
            _emailedevents.AddRange(GetFunReminders(events));

 

 

 

And then had six-plus methods following, all doing the same thing for different .Where(a=>a.lambdas).  Instead, I replaced it with one method, and the six calls just pass the lambdas:

IEnumerable<ACME_WEB.Models.Foo> foos = ACME_WEB.lib.Repository.GetFoos(); 
    List<ACME_WEB.Models.FooNote> _emails = new List<ACME_WEB.Models.FooNote>();
 
    _emails.AddRange(GetNotes(foos, ACME_WEB.Models.NoteType.Fee,
    new Func<ACME_WEB.Models.Foo, bool>(
            f => f.HasFee == false && f.TypeId == ACME_WEB.lib.CONST.FEETYPEID &&
             f.Events.Count(e => e.EventTypeId == ACME_WEB.lib.CONST.FUNID &&
                            e.StartDate == DateTime.Now.Date.AddDays(10)) > 0)));
 
    _emails.AddRange(GetNotes(foos, ACME_WEB.Models.NoteType.Pho,
    new Func<ACME_WEB.Models.Foo, bool>(
           f => f.HasPho == false && f.TypeId == ACME_WEB.lib.CONST.PHOID &&
            f.Events.Count(e => e.EventTypeId == ACME_WEB.lib.CONST.FUNID &&
                            e.StartDate == DateTime.Now.Date.AddDays(1)) > 0)));
// … and then, the single method
private static List<ACME_WEB.Models.Note> GetNotes(
         IEnumerable<ACME_WEB.Models.Foo> allfoos,  
         ACME_WEB.Models.NoteType notetype,
         Func<ACME_WEB.Models.Foo, bool> predicate)
        {
            var emails = from ACME_WEB.Models.Foo d 
                 in System.Linq.Enumerable.Where(allfoos, predicate)
                         select new ACME_WEB.Models.Note(f, notetype);
                 // and that’s it
}

Tags:

Linq

A More Mature User (model)

by MikeHogg 12. August 2012 09:52

My MVVM and MVC User models have usually been a hierarchy of different classes starting with the simplest name/password and adding more properties with each inherited subclass. 

 

(Side Note: I've noticed a tendency to add more properties to different versions of subclasses can get out of hand, mucking up a membership provider, when mixed with Roles, when what is really called for is a Profile provider for all those descriptive properties.  See Decorate Pattern and MS ProfileProvider.)

 

 

 

This time a client requirement surprised me by making even username/password optional, which I've never done.  As a matter of fact, most of my user hierarchy was built to support the base class of required properties using MVC DataAnnotation Required Attributes.  So

 

I rewrote it as one base User with no requireds, and then various subclasses more like ViewModels.  Also I Interfaced the User and changed all my Membership and Repository arguments to the interface.  Now this pattern seems much more flexible, simple, and easily extensible.  Don't know why I didn't see this before.

 

 

public interface IUserModel
    {
        int Id { get; set; }
 
        string EmailAddress { get; set; }
        string Password { get; set; }  
 
        bool Active { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string PhoneNumber { get; set; }
        bool GetsEmail { get; set; }
 
        IEnumerable<RoleModel> Roles { get; set; }
    }
    
    [Serializable]
    public class UserModel : IUserModel
    {
        [Key]
        public int Id { get; set; }
 
        [Display(Name = "Email Address")]
        [StringLength(255)]
        [MyLibrary.Web.Mvc3.Attributes.EmailAddress]
        public virtual string EmailAddress { get; set; }
 
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public virtual string Password { get; set; }
 
        [Display(Name = "Active")]
        public bool Active { get; set; }
 
        [Display(Name = "First name")]
        [StringLength(50)]
        public virtual string FirstName { get; set; }
 
        [Display(Name = "Last name")]
        [StringLength(50)]
        public string LastName { get; set; }
 
        [StringLength(255)]
        [Display(Name = "Phone Number")]
        public string PhoneNumber { get; set; }
 
        [Display(Name = "Gets Emails?")]
        public bool GetsEmail { get; set; }
 
        [Display(Name = "Roles")]
        public IEnumerable<RoleModel> Roles { get; set; }
    }
 
 
    public class LogOnModel : UserModel
    {
        [Required]
        [Display(Name = "Email Address")]
        [StringLength(255)]
        [MyLibrary.Web.Mvc3.Attributes.EmailAddress]
        public override string EmailAddress { get; set; }
 
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public override string Password { get; set; }
 
    }
 
    public class RegisterModel : LogOnModel
    { 
        [Required(ErrorMessage = "Please enter a first name")]
        public override string FirstName { get; set; }
 
        [DataType(DataType.Password)]
        [Compare("Password")]
        [Display(Name = "Confirm Password")]
        public string PasswordConfirm { get; set; }
    }
 
    public class ChangePasswordModel : UserModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Old Password")]
        public string OldPassword { get; set; }
        
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public override string Password { get; set; }
 
        [DataType(DataType.Password)]
        [Compare("Password")]
        [Display(Name = "Confirm Password")]
        public string PasswordConfirm { get; set; }
    }
 
    public class FoundPasswordModel : UserModel
    {
        [Required] 
        public override string EmailAddress { get; set; }
 
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public override string Password { get; set; }
 
        [DataType(DataType.Password)]
        [Compare("Password")]
        [Display(Name = "Confirm Password")]
        public string PasswordConfirm { get; set; }
    }
 
    public class ValidatedUserModel : UserModel
    {
        [Required]        [StringLength(255)]
        [MyLibrary.Web.Mvc3.Attributes.EmailAddress]        public override string EmailAddress { get; set; }
 
        [Required(ErrorMessage = "Please enter a first name")]
        public override string FirstName { get; set; }
 
        public ValidatedUserModel() { }
        public ValidatedUserModel(IUserModel user)
        {
            Active = user.Active;
            EmailAddress = user.EmailAddress;
            FirstName = user.FirstName;
            GetsEmail = user.GetsEmail;
            Id = user.Id;
            LastName = user.LastName;
            PhoneNumber = user.PhoneNumber;
            Roles = user.Roles;
        }
    }

Tags:

MVC | Architecture

Best of Datagrid plugins for web

by MikeHogg 12. August 2012 09:43

After researching several datagrid mechanisms for mvc3 web page, looking for powerfule filtering and sorting and paging built in, I went with actually a javascript implementation called SlickGrid.

 

https://github.com/mleibman/SlickGrid

 

There was another js grid actually that got very positive reviews also.  I think there was a SO question asking for reviews but it's been a while since I did this research and I don't remember the name or why I chose this one over the others, or over .net controls.  I just want to document how I used it for the future. It offers powerfully fast sorting paging and even As You Type filtering on datasets upwards of 100k I am told, and believe, although I have not had need to use it for more than an order of hundreds yet.

 

besides adding the source (i use slick.core, .dataview, .formatters (might be mine), and .pager) to your project, you init the grid like any other plugin.  You set your Columns and options.  Columns here is the important point.

 

 
<link href="@Url.Content("~/Content/slick.grid.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/slick.pager.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Content/js/jquery.event.drag-2.0.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/slick.core.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/slick.grid.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/slick.dataview.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/slick.formatters.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/slick.pager.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/json2.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/slick.mylib.js")" type="text/javascript"></script>
 
<script type="text/javascript">
 
    var columns = [
     { id: "Name", name: "Name", field: "Name", width: 140, sortable: true },
                    { id: "Email", name: "Email", field: "Email", width: 140, sortable: true }, 
     { id: "Type", name: "Type", field: "Type", sortable: true }, 
                    { id: "Title", name: "Title", field: "Title", width: 150, sortable: true },
                    { id: "RequestDate", name: "Request Date", field: "RequestDate", formatter: Slick.Formatters.Date, filter: false, sortable: true },
                    { id: "DueDate", name: "Due Date", field: "DueDate", formatter: Slick.Formatters.Date, filter: false, sortable: true },
                    { id: "AnotherDueDate", name: "Another Due Date", field: "AnotherDueDate", formatter: Slick.Formatters.Date, filter: false, sortable: true },
                    { id: "Details", name: "Details", field: "Id", filter: false,
                        formatter: function (row, cell, value, columnDef, dataContext) {
                            return '<a href="/Home/ViewDetails/' + dataContext['Id'] + '">Details</a>';  }   },
 
                    { id: "selector", name: "Has Approval", field: "HasApproval", formatter: Slick.Formatters.Checkmark, cssClass: "centered", filter: false, sortable: true },
                    { id: "HasDeadline", name: "Has Deadline", field: "HasDeadline", formatter: Slick.Formatters.Checkmark, cssClass: "centered", filter: false, sortable: true },
                    { id: "AddFiles", name: "Add Files", field: "HtmlUploads", cssClass: "icons centered", filter: false,
                        formatter: function (row, cell, value, columnDef, dataContext) {
                            return '<span class="add-files" data-for="#add-files-text-' + dataContext['Id'] + '">Add</span>' +
                            '<span id="add-files-text-' + dataContext["Id"] + '" class="add-files-text">' +
                            value + '</span>'; }   },
 
                    { id: "HasCapital", name: "Has Capital", field: "HasCapital", cssClass: "centered", filter: false, sortable: true, formatter: Slick.Formatters.Checkmark },
                    { id: "DownloadFiles", name: "Download", field: "HtmlDownloads", width: 80, filter: false, formatter: function (row, cell, value, columnDef, dataContext) {
                        return '' + value; }   }
                ];
 
 
                var options = {
                    enableColumnReorder: false,
                    forceFitColumns: true,
                    defaultColumnWidth: 60,
                    autoHeight: true,
                    rowHeight: 60,
                    showHeaderRow: true,
                    headerRowHeight: 37
                };
 
</script>   
 
<h2>Welcome to the Mike Hogg Something Manager</h2>
 
<div class="text clearfix">
    <div class="button-cluster centered wide clearfix">
        @Html.ActionLink("Submit New Something", "NewSomething", null, null, new { @class = "btn" })
        @Html.ActionLink("View Calendar", "Index", "Calendar", null, new { @class = "btn" })
    </div>
    <br />
    <p>Below is a list of submitted somethings currently in your queue. To upload a xyz or abc file, select your something and click on the "Add File" icon. You can also sort the somethings by clicking on the column header, or filter by typing in  stuff</p>
</div> 
 
    <input type="hidden" id="message" value="used in getExport() above"/>
    <div id="myGrid"></div> 
    <div id="myPager"></div> 

 

And then in your js you set up your ajax method to get the json that populates the column, and sets the data to a placeholder control on your page.  I think leiberman builds the sort event into the grid, but you need to write your own comparers, so mine is here.  I think the basic filter was included but needs to be extended for your needs.  I had a need for escaping HTML and found the escape map also on S.O.

 

 

 
var grid;
var sortcol = "RequestDate";
var sortdir = -1;
var columnFilters = {};
var dataView = new Slick.Data.DataView({ inlineFilters: true });
dataView.setPagingOptions({ pageSize: 8 });
 
 
// my freshmen filter
function filter(item) {
    for (var columnId in columnFilters) {
        if (columnId !== undefined && columnFilters[columnId] !== "") {
            var c = grid.getColumns()[grid.getColumnIndex(columnId)];
            //if (item[c.field] != columnFilters[columnId]) {
 
            var field = item[c.field], myfilter = columnFilters[columnId];
            if (field != null && myfilter != null) {
                if (field.toString().toLowerCase().indexOf(myfilter.toString().toLowerCase()) == -1) {
                    return false;
                }
            } else { return field == myfilter; }
        }
    }
    return true;
}
 
// my freshmen attempt at comparer (for SORT)
function comparer(a, b) {
    var x = isNaN(a[sortcol]) ? a[sortcol].toLowerCase() : a[sortcol];
    var y = isNaN(b[sortcol]) ? b[sortcol].toLowerCase() : b[sortcol];
    if (x == null || x == "") {
        if (y == null || y == "") {
            return 0;
        }
        else return -1;
    } 
    else if (y == null || y == "") return 1;
    else return (x == y ? 0 : (x > y ? 1 : -1));
}
 
 
 
var entityMap = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': '&quot;',
    "'": '&#39;',
    "/": '&#x2F;'
};
 
function escapeHtml(string) {
    return String(string).replace(/[&<>"'\/]/g, function (s) {
        return entityMap[s];
    });
}
/// ADDITIONAL

 

We had some cute image links for a popup menu in one of the cells but as you scroll and page through the grid, they needed to be recreated as this grid implementation actually dropped and added rows on and off the dom on the fly, so LoadButtons ...

 

 
// for those pencil icon popup menus for Add Files
function loadButtons() {
 
    $(".add-files-text").dialog({
        autoOpen: false,
        title: "Add Files"
    });
    $(".add-files").button({
        icons: { primary: 'ui-icon-pencil' },
        text: false
    });
    $(".add-files").click(function () {
        $($(this).attr("data-for")).dialog("open");
        return false;
    });
}
 
 
// adds those filters to each column, special here to not add for certain columns, extended by 'filter:'
function updateHeaderRow() {
    for (var i = 0; i < columns.length; i++) {
        var header = grid.getHeaderRowColumn(columns[i].id);
        $(header).empty();
        if (columns[i].id !== "selector" && columns[i].filter != false) {
            $("<input type='text'>")
                    .data("columnId", columns[i].id)
                    .val(columnFilters[columns[i].id])
                    .appendTo(header);
        } else {
            $("<div><span style='height:39px;display:block;'></span></div>").appendTo(header);
        }
    }
    if (grid.getOptions()["excelExport"] == true) {
        // add Excel button
        var header = grid.getHeaderRowColumn(columns[columns.length - 1].id);
        $(header).empty();
        $("<input type='image' src='../Content/img/Excel-icon.png' title='Export to Excel' onclick='getExport();'/>").appendTo(header);
    }
}
 
 
 
function getExport() {
    $.ajax({
        url: '/Home/DeploymentList',
        type: "post",
        data: JSON.stringify(dataView.getFilteredRows()),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (result) { getCSVFileFromFormPost(result); },  // ajax post json data which server can read automatically, responds with a string, which ajax then POSTs inside a FORM, to which the server adds headers and returns, so browser can treat it as a download (save/open)
        error: function (xhr, textStatus, errorThrown) {
            $("#message").html('Error occurred, ReadyState: ' + xhr.readyState +
                '; textStatus: ' + textStatus + '; ' + errorThrown);
        }
    });
    return false;
}
 
function getCSVFileFromFormPost(result) {
    $('<form action="/Home/GetCSVFile" method="post"><input type="hidden" name="csv" id="csv" value="' + escapeHtml(result) + '" /></form>').appendTo("body").submit();
}
 

 

The Export to CSV function was a neat one, since we could send the dataset back to the server in json easily, and in MVC we could set up an Action to Deserialize that same json to a List of Models if we lined everything up right.

 

And "the call"...

 

$(function () {
    $.post('/Home/GetDepData', function (data, textstatus) {
 
        grid = new Slick.Grid("#myGrid", dataView, columns, options);
        var pager = new Slick.Controls.Pager(dataView, grid, $("#myPager"));
 
        grid.onSort.subscribe(function (e, args) {
            sortdir = args.sortAsc ? 1 : -1;
            sortcol = args.sortCol.field;
 
            if ($.browser.msie && $.browser.version <= 8) {
                // using temporary Object.prototype.toString override
                // more limited and does lexicographic sort only by default, but can be much faster
 
                dataView.fastSort(sortcol, args.sortAsc);
            } else {
                // using native sort with comparer
                // preferred method but can be very slow in IE with huge datasets 
                dataView.sort(comparer, args.sortAsc);
            }
        });
 
        // wire up model events to drive the grid
        dataView.onRowCountChanged.subscribe(function (e, args) {
            grid.updateRowCount();
            grid.render();
 
            loadButtons();  // when filtered to fewer rows than before and no rows were changed
        });
 
        dataView.onRowsChanged.subscribe(function (e, args) {
            grid.invalidateRows(args.rows);
            grid.render();
 
            loadButtons();  // when filtered or paged
        });
 
        dataView.onPagingInfoChanged.subscribe(function (e, pagingInfo) {
            var isLastPage = pagingInfo.pageNum == pagingInfo.totalPages - 1;
            var enableAddRow = isLastPage || pagingInfo.pageSize == 0;
            var options = grid.getOptions();
        });
 
 
        $(grid.getHeaderRow()).delegate(":input", "change keyup", function (e) {
            columnFilters[$(this).data("columnId")] = $.trim($(this).val());
            dataView.refresh();
        });
 
        // initialize the model after all the events have been hooked up
        dataView.beginUpdate();
        dataView.setItems(data, "Id");  // Id capital I to match my unique model id property
        dataView.setFilter(filter);
        dataView.endUpdate();
 
        updateHeaderRow();
        loadButtons();
    }, "json");
});

 

At the end of the included slick.formatters.js I added a couple of my own, the javascript time function being a keeper...

 

    ...
    function YesNoFormatter(row, cell, value, columnDef, dataContext) {
        return value ? "Yes" : "No";
    }
 
    function CheckmarkFormatter(row, cell, value, columnDef, dataContext) {
        return value ? "<img src='../Content/img/slickimages/tick.png'>" : "";
    }
    function DateFormatter(row, cell, value, columnDef, dataContext) {
        if (value !== null) {
            var d = new Date(parseInt(value.substr(6, 13)));
            return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear() + ' ' + getAMPMTime(d);
        } else return "";
    }
 
    Number.prototype.pad = function (len) {
        return (new Array(len + 1).join("0") + this).slice(-len);
    }
 
    function getAMPMTime(d) {
        var d = new Date(d);
        var hour = d.getHours();
        var min = d.getMinutes().pad(2);
        var ap = "AM";
        if (hour > 11) { ap = "PM"; }
        if (hour > 12) { hour = hour - 12; }
        if (hour == 0) { hour = 12; }
 
        if (hour == 12 && min == 0 && ap == "AM") return '';
 
        return hour + ':' + min + ' ' + ap;
    }

Tags:

JQuery | MVC | Javascript

CheckboxLists in MVC3

by MikeHogg 2. August 2012 09:48

 

MVC does not really model bind checkboxes. The problem is html based; input checkboxes do not post in the form unless actually checked. MVC gets around this with their helper by creating a hidden for every checkbox, and I assume unobstrusively sets the hidden input whenever the associated checkbox is toggled.

 

This means that we cannot validate the checkbox as required, which isn't an easy case anyway since a bool defaults to false. It’s not a nullable type.  And we can't readily access the hidden input to validate that.  People have created several roll-your-own extensions and helpers out there… but I couldn’t find one that fit my needs exactly.

 

My main requirement was a list of boxes (a grouping so to speak) requiring at least one to be checked. With client validation (most examples on the web missed this one).  SO I found myself a little library project.

 

Two parts to this-

  • - [In your Model]: add your list of checkboxes like this: IEnumerable<MH.Mvc.Checkbox> { new {Name="Bank"} new {Name="Stocks", IsChecked?="true"}}
    • decorate your checkboxlist with [MH.Attributes.CheckBoxListServerValidator], and you will get server validation that at least one box is checked
    • In your View, use the HtmlHelper.CheckBoxListFor(m=>m.checkboxlist) which gives you the proper model binding
  • - [In your Model]: add a bool named something like this: CheckboxClientValidator
    • decorate your bool with [MH.Attributes.CheckBoxListClientValidator(listname)], and
    • add the MH/Content/scripts/custom-validators js to your page and you will get client side validation that at least one box is checked

Here is what my Model looks like:

 

 

[MH.Web.Mvc3.Attributes.CheckBoxListClientValidator("LiquidAssetSourceList")]
        public bool LiquidAssetClientValidator { get; set; }    
        [Display(Name = "Where do you have your liquid assets? (Check all that apply.)*")]
        [MH.Web.Mvc3.Attributes.CheckBoxListServerValidator]
        public IEnumerable<MH.Web.Mvc3.Models.CheckBox> LiquidAssetSourceList { get; set; }

 

In my Model constructor (isn't that where all lists should get instantiated?) I create the actual checkboxes

 

 

LiquidAssetSourceList = new List<MH.Web.Mvc3.Models.CheckBox>  
                {   new MH.Web.Mvc3.Models.CheckBox{ Name = "Bank" },
                    new MH.Web.Mvc3.Models.CheckBox{ Name = "Stocks" },

 

... and Here is what my View looks like:

 

@Html.LabelFor(model => model.LiquidAssetSourceList)
@Html.HiddenFor(m=>m.LiquidAssetClientValidator)
@Html.ValidationMessageFor(m=>m.LiquidAssetClientValidator)
@Html.CheckboxListFor(m=>m.LiquidAssetSourceList)
Here's the simple checkbox object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MH.Models
{
    public class CheckBox
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsChecked { get; set; } 
    }
}

 

And the HtmlExtension:

  public static System.Web.Mvc.MvcHtmlString CheckboxListFor<TModel, TProperty>(this HtmlHelper<TModel> html,
            Expression<Func<TModel, TProperty>> checkboxlist, object htmlattributes = null)
             where TProperty : IEnumerable<Models.CheckBox>
        {
            MemberExpression body = checkboxlist.Body as MemberExpression;
            string checkboxlistname = body.Member.Name;
 
            //Get currently selected values from the ViewData model
            IEnumerable<Models.CheckBox> cbs = checkboxlist.Compile().Invoke(html.ViewData.Model);
 
            StringBuilder sb = new StringBuilder();
            int idx = 0;
            foreach (Models.CheckBox cb in cbs)
            {
                string id = String.Format("{0}_{1}__IsChecked", checkboxlistname, idx);
                string name = String.Format("{0}[{1}].IsChecked", checkboxlistname, idx);
 
                TagBuilder itemdiv = new TagBuilder("div");
                itemdiv.AddCssClass("checkboxlist-itemdiv");
 
                TagBuilder label = new TagBuilder("label");
                label.AddCssClass("checkboxlist-label");
                label.MergeAttribute("for", id);
                label.InnerHtml = cb.Name;
                itemdiv.InnerHtml += label.ToString(TagRenderMode.Normal);
 
                TagBuilder inputcheck = new TagBuilder("input");
                if (cb.IsChecked) inputcheck.MergeAttribute("checked", "checked");
                inputcheck.AddCssClass("checkboxlist-checkbox");
                inputcheck.MergeAttribute("type", "checkbox");
                inputcheck.MergeAttribute("id", id);
                inputcheck.MergeAttribute("name", name);
                inputcheck.MergeAttribute("value", "true");
                itemdiv.InnerHtml += inputcheck.ToString(TagRenderMode.Normal);
 
                TagBuilder hiddencheck = new TagBuilder("input");
                hiddencheck.MergeAttribute("type", "hidden");
                hiddencheck.MergeAttribute("name", name);
                hiddencheck.MergeAttribute("value", "false");  // input checkboxes only post if checked, so we default to hidden with same id... mvc only takes first input of each id
                itemdiv.InnerHtml += hiddencheck.ToString(TagRenderMode.Normal);
 
                TagBuilder hiddenname = new TagBuilder("input");
                hiddenname.MergeAttribute("type", "hidden");
                hiddenname.MergeAttribute("name", String.Format("{0}[{1}].Name", checkboxlistname, idx));
                hiddenname.MergeAttribute("value", cb.Name);
                itemdiv.InnerHtml += hiddenname.ToString(TagRenderMode.Normal);
 
                TagBuilder hiddenid = new TagBuilder("input");
                hiddenid.MergeAttribute("type", "hidden");
                hiddenid.MergeAttribute("name", String.Format("{0}[{1}].ID", checkboxlistname, idx));
                hiddenid.MergeAttribute("value", cb.ID.ToString());
                itemdiv.InnerHtml += hiddenid.ToString(TagRenderMode.Normal);
 
                sb.Append(itemdiv.ToString(TagRenderMode.Normal));
                idx++;
            }
 
            TagBuilder div = new TagBuilder("div");
            div.AddCssClass("checkboxlist-paneldiv");
            div.MergeAttribute("id", checkboxlistname);
            div.MergeAttribute("name", checkboxlistname);
            div.MergeAttributes(new System.Web.Routing.RouteValueDictionary(htmlattributes));
            div.InnerHtml = sb.ToString();
 
            return new System.Web.Mvc.MvcHtmlString(div.ToString(TagRenderMode.Normal));
        } 

The attribute for the validator:

 

Namespace Attributes{
[AttributeUsage(AttributeTargets.Property)]  
    public class CheckBoxListServerValidator : ValidationAttribute 
    { 
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null) 
            {  
                if ((value as IEnumerable<Euro.Web.Mvc3.Models.CheckBox>).Any(l => l.IsChecked)) return ValidationResult.Success;
            }
            return new ValidationResult("Please select an option.");
        }         
    }
    [AttributeUsage(AttributeTargets.Property)]  // validon bool? i dont think it matters
    public class CheckBoxListClientValidator : ValidationAttribute, IClientValidatable
    {
        public string ListName { get; set; }
        public CheckBoxListClientValidator(string listname)
        {
            ListName = listname;
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        { 
            return ValidationResult.Success; 
        }
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule()
                {
                    ValidationType = "requiredcheckbox", // "requiredcheckbox" used in jquery
                    ErrorMessage = "Please select at least one"
                };
            rule.ValidationParameters.Add("listname", ListName);
            return new[] { rule };
        }
    }

and the js for the validator:

In custom-validators.js
/// (do not put validators inside document.ready)
/// these two are for MH.Attributes.CheckBoxListClientValidator 
$.validator.addMethod("requiredcheckbox",
                function (value, element, parameters) {
                    return $("#" + parameters.listname + " input:checked").length > 0;
                });
$.validator.unobtrusive.adapters.add("requiredcheckbox", ["listname"], function (options) {
    options.rules["requiredcheckbox"] = options.params;
    options.messages["requiredcheckbox"] = options.message;
}); 

Tags:

MVC

About Mike Hogg

Mike Hogg is a c# developer in Brooklyn.

More Here

Favorite Books

This book had the most influence on my coding style. It drastically changed the way I write code and turned me on to test driven development even if I don't always use it. It made me write clearer, functional-style code using more principles such as DRY, encapsulation, single responsibility, and more. amazon.com

This book opened my eyes to a methodical and systematic approach to upgrading legacy codebases step by step. Incrementally transforming code blocks into testable code before making improvements. amazon.com

More Here