Posts

Showing posts from August, 2015

Build an ActionLink with a Value from the Page

Normally I would use an Html.BeginForm and wrap my link inside it to force it to the controller to handle any variables, but every so often you may need to add a link on the page which is populated with a variable from a textbox and you choose not to post back to the controller. In this example I want to pass an account number in my ActionLink So I start by adding the textbox on the page  Enter Account Number: @Html.TextBox("account") And the Actionlink (using some bootstrap formatting)   @Html.ActionLink("View", "Customer", null, new { id = "View", @class="btn btn-danger" }) Then I handle the click event in script on the page <script>     $(function () {         $('#View').click(function () {             var fran = $('#account').val();             this.href = this.href + '?id=' + encodeURIComponent(fran);         });     }); </script> My link now resolves to something like thi

Pivot Data and Roll up 12 months of Summaries

Image
The need was for a report that rolled up sales by item by month for the year on the MVC Razor page.  Here is how I did it. First I created a Model that looked like this   public  class DTO_SkuReport     {         public string SKU { get; set; }         public int Month { get; set; }         [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]         public decimal linetotal { get; set; }         public decimal JanTotal { get; set; }         public decimal FebTotal { get; set; }         public decimal MarTotal { get; set; }         public decimal AprTotal { get; set; }         public decimal MayTotal { get; set; }         public decimal JunTotal { get; set; }         public decimal JulTotal { get; set; }         public decimal AugTotal { get; set; }         public decimal SepTotal { get; set; }         public decimal OctTotal { get; set; }         public decimal NovTotal { get; set; }         public decimal DecTotal { get; set; }  

The Beautiful Razor WebGrid

The webgrid is truly a work of art.  The fastest way to show results of a query in a friendly, sortable, easily formattable fashion. Conditionally formatting our data can sometimes offer struggles so here are a couple of tips. Have a pesky nullable date column? You can add something like this inside your grid - here I am working with both a status and a nullable date.  So if the status equals "NA"  (I set that in my query if the step wasn't relevant), I am returning a black box into the cell otherwise i am returning the sew date (item.Sew) which can be nullable - here I am returning an empty cell if null or a nicely formatted date if not. IF STATEMENT IN GRID  ,grid.Column(header:"Sew", format: (item) =&gt;                 {                     if (item.SewStatus == "NA")                     { return Html.Raw(string.Format("<text><img height=40 width=100% src=\"{0}\" alt=\"Image\"/></text>&q