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