I struggled a bit attempting to generate dynamic javascript in an MVC Razor page view.  I kept getting the following error:  Too many characters in character literal.

I did a little research and found the answer.  You will need to include
@using System.Text; at the top of your page view.

To build my javascript, I included the following code block within my <script> tags:

@{
StringBuilder sb = new StringBuilder();
foreach (GetBillingCodes_Result billingCode in Model.BillingCodes)
{
sb.Append(“AddBillingCode(‘” + billingCode.billing_code + “’”);
sb.Append(“, ‘” + billingCode.travel + “’”);
sb.Append(“, ‘” + billingCode.billing_code_type + “’”);
sb.Append(“, ‘” + billingCode.billing_rate + “’”);
sb.Append(“, ‘” + billingCode.taxable + “’”);
sb.Append(“, ‘” + billingCode.discount + “’”);
sb.Append(“, ‘” + billingCode.C100_club + “’”);
sb.Append(“, ‘” + billingCode.active + “’”);
sb.Append(“);”);
}
@MvcHtmlString.Create(sb.ToString());
}

Advertisements