Posts

How to Enable jQuery Client-Side Validation in Dynamically Rendered Partial Views in ASP.NET MVC

Recently I was doing some work in an ASP.NET MVC 4 website and was rendering a partial view to a div element via a jQuery AJAX call. I found that when the partial was included in a view from a controller's action method, all the client-side validation worked correctly, but if I rendered the partial view client-side code that was, for example, just getting the partial view by itself and putting into a div element as I was trying to do, the client-side validation wouldn't work. After doing a little research, I learned that the client-side validation needed to be reset after the partial was rendered via the client-script. To that end, I wrote the following JavaScript function to allow quick and easy resetting of client-side validation in cases such as these. Just pass the CSS selector to the partial's parent form to the function shown below. This, of course, assumes that you also have references to the jQuery unobtrusive validation library. function  ResetValidation(formSe...

An ASP.NET MVC ActionButton HtmlHelper Extension Method

Here's a quick ActionButton HtmlHelper extension method I wrote. Hopefully this will come in handy to someone. For more information on extension methods, check out Microsoft's MSDN document here , or check out one of my earlier posts here. public   static   MvcHtmlString  ActionButton(      this   HtmlHelper  htmlHelper,  string  value,  object  htmlAttributes,  string  actionName,       string  controllerName,  object  routeValues) {      if  ( String .IsNullOrWhiteSpace(actionName))          throw   new   ArgumentException ( "actionName is required." );      if  ( String .IsNullOrWhiteSpace(controllerName))          throw   new   ArgumentException ( "controllerName is required." );   ...

Odd Doctor Who VHS Tape Cover

Image
In my early teen years, I used to watch Dr Who on our local PBS affiliate on Saturday afternoons. I haven't seen it in years, but for some reason I was reminded of it one day this week. Later that same day, a co-worker mentioned it to me, and then a day or two later someone else brought it up. I thought it was kind of funny that this TV show I hadn't watched since I was a teenager was now frequently popping up. This, in turn, reminded me of a particularly odd VHS tape cover I'd seen when I was still a teenager. The third actor to play Dr. Who was a gentleman named Jon Pertwee. It seems like everyone's favorite Dr. Who is Tom Baker, who was the fourth actor to play the part, but I never saw much of Tom Baker's work, and my personal favorite is Jon Pertwee. And at some point, a VHS release of some of his best work was released, but it had an extremely odd photo of Mr. Pertwee on the cover:   I'm not sure what they were thinking when they chose this phot...

A Generic Method for Consuming Data From RESTful WCF Service Methods

I recently added some new methods to an existing RESTful WCF service and needed to consume these methods from an application which had not previously made use of this service. To make this easier, I created the following generic method. It could probably use a little refinement (specifically some code to handle what happens if an attempt is made to cast the service method result to an incorrect type), but it does the trick. using  System.IO; using  System.Net; using  System.Runtime.Serialization; using  System.Text; namespace  RESTService.Helpers {      public   static   class   RESTServiceHelpers     {          public   static  T GetResultFromRESTServiceMethod<T>( string  url)         {             HttpWebRequest request = (...

Using Reflection to Get the Value of a Nested Property

I recently ran into a situation wherein I needed to use reflection to get the value of a nested property. We were returning a custom type that inherits from JsonResponse, but this custom type is private to the controller class that uses it (I'm not sure why -- since we're returning it from some action methods I'd have thought it could be public. But I digress). When writing some unit tests for these methods, I wanted to check the value of the success property which just so happens to be nested within the Data property. So I wrote a little method to assist with this, as shown below (the method and class names have been changed to protect the innocent). The steps to follow are: Get the type of the object. Using the type, get the parent property of the nested property you want via Type.GetProperty(), specifying the name of the parent property as the parameter. Using the property, get its value using PropertyInfo.GetValue(), specifying the parent object and null as pa...

A Simple Method to Get an Object's Properties and Values at Runtime

I recently wrote some code to integrate with a WCF service authored by a different team and encountered an error, happening internal to the service, during my initial integration testing of one of the methods. I wanted to provide the developer on the other team with the parameters I passed into the method when the exception occurred, and one of those parameters was an object with more than a few properties. To that end, I wrote a quick method to provide the object's properties and their values so I could include it in the exception handling on my end and provide this info to the other developer to aid in debugging. This method requires a using statements for System.Reflection and System.Text. It could also be expanded a bit (for example, in the way it represents the value for property values that are objects), but as a quickly-written method it gets the job done. protected   string  GetObjectPropertiesAndValues( object  target) {      StringBuilder...

Setting a log4net AdoNetAppender Connection String at Runtime

I recently implemented log4net in a project to log to a database using the log4net AdoNetAppender class. But I had a need to be able to specify the connection string at runtime. Here's how I was able to do it. Hopefully this will help out someone who has the same need. :) protected   void  SetLog4NetAppenderConnectionString( string  connectionString) {     log4net.Repository.Hierarchy. Hierarchy  hierarchy =         log4net. LogManager .GetLoggerRepository()               as  log4net.Repository.Hierarchy. Hierarchy ;      if  (hierarchy !=  null )     {         log4net.Appender. AdoNetAppender  appender             = (log4net.Appender. AdoNetAppender )hierarchy.GetAppe...