Sunday, January 28, 2018

Form Collection 3 approaches

Here are 3 ways to do it specifically with a FormCollection object.

public ActionResult SomeActionMethod(FormCollection formCollection)
{
  foreach (var key in formCollection.AllKeys)
  {
    var value = formCollection[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 { wo.OrderID, wo.FranchiseID } equals
                            new { wot.OrderID, wot.FranchiseID }

                              where (wo.WSADate >= startdate && wo.WSADate <= enddate)
                                && wot.AcceptedYN != false
                              where ((wo.DispatchCode == "DVC") || (wo.DispatchCode.Substring(0, 10) == "Dryer Vent")) ||
                             ((filterList.Contains(wot.TaskCodeNumber) || wot.TaskCodeNumber == "00"))

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) });
                    }
                }
            };
            //foreach (var pS in notNullProps.GetType().GetProperties())
        }

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>