Posts

Showing posts from January, 2014

CRM 2011: How do I validate that a text field is numeric?

I need to change the format of an ID field in CRM 2011. The system settings were inserting commas as thousands separators and this looks strange for an ID (ID 123,456 is just not right). I recreated the field as a text field, but I needed to validate the input.I therefore created the following functions that I added to the 'onSave' list of my CRM form: When configuring the script, ensure that 'pass context as the first parameter' is set and then pass in the name of the field to search (in double quotes). Here is the script: validateIDOnSave = function(executionObj, IDField) {     var IDFieldValue = Xrm.Page.getAttribute(IDField).getValue();     if(IDFieldValue != null)     {       var IDFieldObject = Xrm.Page.ui.controls.get(IDField);       if (isNumber(IDFieldValue ) == false)        {                 executionObj.getEventArgs().preventDefault(); ...

MVC 4: How do I download a list as a CSV?

I added a simple link in my view @Html.ActionLink("Export Data", "ExportData") and a simple method in my controller to take care of the download public ActionResult ExportData() { return DownloadCSV(); } public FileContentResult DownloadCSV() { string csv = string.Concat(from x in db.MyData   select x.ID + ","   + x.Name + "\n"); return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv",  string.Format("Export.{0}.csv", DateTime.Now.ToString("yyyy-MM-dd_HHmmss"))); }

MVC 4: How do I line-wrap my text in an editor?

The solution is to add an annotation to the field in the model class: [DataType(DataType.MultilineText)] Then, to display the item in the view, use Html.Raw. @Html.Raw("<pre>"+ Html.Encode(@Model.MyField) + "</pre>"

MVC 4: How do I generate a pdf file from a view?

I looked at several possible solutions (including itextsharp) but there were always issues rendering the CSS correctly. Fortunately, I found the following gem in codeplex:  http://pdfgenerator.codeplex.com/ I used the following code to generate the HTML from the view (sourced from here ):  public string RenderViewToString(Controller controller, string viewName, object viewData)         {             var renderedView = new StringBuilder();             using (var responseWriter = new StringWriter(renderedView))             {                 var fakeResponse = new HttpResponse(responseWriter);                 var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);                 var fakeControllerContext = new Controll...