Thursday, June 8, 2017

MVC Caching issue

Issue:

I had an issue with a MVC view rendering a modal page.  The problem was that the AJAX call was not refreshing the view when it was re-rendered on the page.  The change to the database is occurring but the view refresh was not showing the latest information.

 var data = { "claimNumber": activeClaimID };
        $.ajax
        (
                 {
                     url: '@Url.Action("IndexPartial", "CaseFileViewModels")',
                     async: false,
                     type: 'GET',
                     data: data,
                     success: function (result) {

                         hideWaitDialog();
                       
                         //Clear out the entire modal block
                         $('#modal-container').empty();
                         $('#modal-container').append(result);
                         ret = true;
                     },
                     error: function (xhr, status, err) {
                         alert(xhr.responseText);
                         alert(status);
                     },
                 }
         );


My problem was that I did not assign the attribute Output Cache to the ActionResult.
Once I disabled the default hander on ActionResult then my view began to refresh.

[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
public ActionResult IndexPartial()
{....}


Source:

https://stackoverflow.com/questions/20917398/view-not-refreshing-after-ajax-call


Tuesday, June 6, 2017

Jquery attr method


Issue

I had a checkbox with imbedded attributes.  I needed to get these values out of based on a click event
The key to getting the specified values was the use of the attr method.

<input name="SavePaySchedule" id="SavePaySchedule1" type="checkbox" value="true" selectedscheduleitem="1f3dc523-c466-4dc5-bb52-09848a79c9fd" schedulecountry="44207e0b-d4ca-4bfd-b1b3-2f28a2c6d5ef">


 <script type="text/javascript">
                                    $('#SavePaySchedule').click(function (o) {
                                        var $input = $(this);
                                        var scheduleCountry = $input.attr('scheduleCountry');
                                        var selectedScheduleItem = $input.attr('selectedScheduleItem');
                                        var activeState = $input.prop('checked');

                                    });
                                </script>

Source

http://api.jquery.com/attr/

Linq to SQL distinct

Issue:

I needed to get a distinct value from a query like

select distinct o.ProgramID,t.Program
from table1 o
inner join table2 t on o.ProgramID=t.ProgramID
order by t.Program

var result = (from o in table1
              join t in table2 on o.ProgramID equals t.ProgramID
              orderby t.Program
              select new { o.ProgramID, t.Program }).Distinct();
https://stackoverflow.com/questions/4318909/linq-to-sql-distinct-and-orderby

Wednesday, April 19, 2017

MVC razor decimal formatting

Issue:

Trying to get a decimal property of an EF generated model to display more than default 2 decimal place accuracy.

Use N6 as the numeric format string.
myDecimal.ToString("N6");
or
string.Format("{0:N6}", myDecimal);

Source:

http://stackoverflow.com/questions/8412882/c-sharp-show-a-decimal-to-6-decimal-places

The entity cannot be constructed in a LINQ to Entities query

Issue:


I had a problem casting an anonymous type to a type of a ModelView class that I had defined.
The query worked but failed on my conversion to the type PayScheduleCurrencyModelView.

Incorrect:

 var currencyDetail = (from hn in db.Host_Nation
                                      join cur in db.Currencies on hn.Currency_ID equals cur.Currency_ID
                                      where hn.Host_Nation_ID.Equals(country)
                                      select new PayScheduleCurrencyModelView
                                      {
                                          Country_ID = hn.Host_Nation_ID,
                                          Country = hn.Host_Nation_Name,
                                          Currency_Code = cur.Code,
                                          Currency_Description = cur.Description,
                                          Exchange_Rate = cur.Rate
                                      }).AsEnumerable().Select(x => new PayScheduleCurrencyModelView
                                      {
                                          Country_ID = x.Country_ID,
                                          Country = x.Country,
                                          Currency_Code = x.Currency_Code,
                                          Currency_Description = x.Currency_Description,
                                          Exchange_Rate = x.Exchange_Rate
                                      }).FirstOrDefault();


Correct:


 var currencyDetail = (from hn in db.Host_Nation
                                      join cur in db.Currencies on hn.Currency_ID equals cur.Currency_ID
                                      where hn.Host_Nation_ID.Equals(country)
                                      select new 
                                      {
                                          Country_ID = hn.Host_Nation_ID,
                                          Country = hn.Host_Nation_Name,
                                          Currency_Code = cur.Code,
                                          Currency_Description = cur.Description,
                                          Exchange_Rate = cur.Rate
                                      }).AsEnumerable().Select(x => new PayScheduleCurrencyModelView
                                      {
                                          Country_ID = x.Country_ID,
                                          Country = x.Country,
                                          Currency_Code = x.Currency_Code,
                                          Currency_Description = x.Currency_Description,
                                          Exchange_Rate = x.Exchange_Rate
                                      }).FirstOrDefault();


The fix was to drop the implied cast in the linq to sql query.  This cast was causing the EF error.


Source:


  1. http://stackoverflow.com/questions/12916080/the-entity-or-complex-type-cannot-be-constructed-in-a-linq-to-entities-query

Friday, April 14, 2017

Set variable formatting in MVC razor

Issue

This was a real challenge to figure out.  It seems pretty straight forward but it is the details that eat up so much time.  I had a number of decimal data types that represented currency.  I wanted them displayed at local currency.  I thought .Net has this down and lots of people have done this already.  Easy, right?

Well, yes and no.  I went to implement and found most examples and answers incomplete.

Here is what you need.  In the razor view, add references to the threading and globalization namespaces:

@using System.Threading
@using System.Globalization

In your markup, use the String.Format and CultureInfo methods to get your formatting correct.

 var tDebit = String.Format(new CultureInfo("en-US"), "{0:C}", nDebit);
 @Html.Raw(tDebit);



Sources

  1. https://msdn.microsoft.com/en-us/library/bz9tc508.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3
  2. http://stackoverflow.com/questions/10740828/format-decimal-to-currency-in-viewbag
  3. https://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Tuesday, April 4, 2017

Print content of javascript object

Issue:

The issue I had was listing the contents of js object.  This is pretty straightforward but handy.

alert(JSON.stringify(YOUR_OBJECT_HERE, null, 4));

Source:

http://stackoverflow.com/questions/1625208/print-content-of-javascript-object