Posts

Showing posts from January, 2018

Form Collection 3 approaches

Here are 3 ways to get your values with a FormCollection object. public ActionResult SomeActionMethod ( FormCollection formCollection ) { //Cycling through the AllKeys foreach ( var key in formCollection . AllKeys ) { var value = formCollection [ key ]; } //By Each Key foreach ( var key in formCollection . Keys ) { var value = formCollection [ key . ToString ()]; } // Using the ValueProvider IValueProvider valueProvider = formCollection.ToValueProvider(); foreach (string key in formCollection.Keys) { ValueProviderResult result = valueProvider.GetValue(key); string value = result.AttemptedValue; } }

Linq and Lambda syntax

var jsonData = from user in users select new [] { user . Id . ToString (), user . Person . Lastname , user . Person . Firstname }; Alternatively, you can use the lambda syntax: var jsonData = users . Select ( user => new [] { user . Id . ToString (), user . Person . Lastname , user . Person . Firstname });

Join Linq To list outside of database

    var filterList = new List<string>();             filterList.Add("OO172222");             filterList.Add("OO172232");             filterList.Add("OO172242");             filterList.Add("OO172252");             filterList.Add("OO172262");             filterList.Add("OO172272");             filterList.Add("OO172282");             filterList.Add("OO172302");             filterList.Add("OO172312");             filterList.Add("OO172322");             var rptRecords = (from wo in BucketEntities.tbl_SalesWorkOrder                               join fi in BucketEntities.FranchiseInfoes on wo.FranchiseID equals fi.FranchiseID                               join pc in BucketEntities.tbl_Pivotal_Company on fi.AccountCode equals                                   pc.Franchisee_Account_Code                               join wot in BucketEntities.tbl_SalesWorkOrderTasks on new

Formatted object title

We often use the title to scroll over for more information.  In this example I was tasked with showing a complete address on scroll over.  I wanted to drop HTML into the title.  I used the MvcHtmlString  and the String Format and the Html.Encode methods to accomplish this. MvcHtmlString s = MvcHtmlString.Create(String.Format("{0}\n{1}\n{2},{3} {4}", c.division.divisionName, c.division.street, c.division.city, c.division.stateCountry.stateCountryName, c.division.postalCode));  <li  title='@Html.Raw(Html.Encode(s))'>@c.division.company.companyName</li>                                        

Static Copy Method

I keep this in a static class called ManipulateModels   public static void CopyTo(this object S, object T)         { //Works fine however if a default value is set then it will need to be handled or it will be reset             var notNullProps = S.GetType().GetProperties()                                         .Where(x => x.GetValue(S, null) != null);             foreach (var pS in S.GetType().GetProperties())             {                 foreach (var pT in T.GetType().GetProperties())                 {                     //example for Default Value                     //had to do this due to setting a default value of 0                     //if (pT.Name == "BuildingID") continue;                     if (pT.Name != pS.Name) continue;                     object x = pS.GetValue(S, null);                     if (x != null)                     {                         (pT.GetSetMethod()).Invoke(T, new object[] { pS.GetGetMethod().Invoke(S, null) });  

Razor Web Grid with a ForEach Column

, grid.Column("Roles", header: "Level", format: (item) => { string roletemp = string.Empty; foreach (var x in item.systemUserRoles) { roletemp += x.systemRole.roleDescription + " "; } return new HtmlString(roletemp); })

Declining Character Textbox

@Html.TextAreaFor(model => model.agreement.purpose, new { maxlength = "500", id = "purpose", @class = "form-control", @rows = 5 }) <div style="font-size:11px; color:##999; font-weight:normal;"><span id="PurposeCounter">500</span> Characters remaining</div>  <script language="javascript">         function updatePurpose() {             var maxLength = 500;             summary = document.getElementById("purpose");             if (summary.value.length > maxLength) {                 summary.value = summary.value.substring(0, maxLength);             }             else {                 $("#PurposeCounter").html(maxLength - $("#purpose").html().length);             }         };         $("#purpose").keyup(function () {             updatePurpose();         });         updatePurpose();     </script>

Highlight Search Term

Add a 'searchable' class to the fields you want to search Use this in the doc   $(document).ready(function () {         if (search_term != null) {             search_term = search_term.trim();               var term = search_term;           term = term.replace(/(\s+)/, "(<[^>]+>)*$1(<[^>]+>)*");             var pattern = new RegExp("(" + term + ")", "gi");               $('#searchResults .searchable').each(function () {                       var src_str = $(this).html()                 src_str = src_str.replace(pattern, "<mark>$1</mark>");                 src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/, "$1</mark>$2<mark>$4");                 $(this).html(src_str);               });     }

Dropdown List from JsonResult

In your controller pass back data from a JsonResult method public JsonResult allactiveprograms() { var programs = programProvider.getActivePrograms(); List mylist = new List (); foreach (var p in programs) { program d = new program(); d.programID = p.programID; d.programName = p.programName; mylist.Add(d); } return Json(new SelectList(mylist.ToArray(), "programid", "programname"), JsonRequestBehavior.AllowGet); } On your page  - your script will look something like this: $(document).ready(function(){ if ($("#SearchType :selected").text() == "Program") { $('#Programs').html(""); $('#ddl').css('display', ''); $.getJSON($('#rootpath').val() + '/piaresults/allactiveprograms', function (data) { var items = 

Validating Domestic and International Phone numbers

Validation of both International and Domestic phone numbers presents it's own special challenge.  I came across a regex that handles this pretty well. It matches these cases: +42 555.123.4567 +1-(800)-123-4567 +7 555 1234567 +7(926)1234567 (926) 1234567 +79261234567 926 1234567 9261234567 1234567 123-4567 123-89-01 495 1234567 469 123 45 67 89261234567 8 (926) 1234567 926.123.4567 415-555-1234 650-555-2345 (416)555-3456 202 555 4567 4035555678 1 416 555 9292 While rejecting these: 926 3 4 8 800 600-APPLE I located it here - would love to take credit but alas did not create it: http://www.regexr.com/38pvb And here is the Regex ^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$ Or as a Data Annotation [RegularExpression(@"^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$",ErrorMessage ="Please match a valid international or domestic phone format&quo