Category Archives: Visual Studio

Realtime MVC ModelState errors during debugging

While working on my most recent ASP.NET MVC project, I was having a tough time figuring out why my ModelState was invalid. It was due to the fact that I set the error messages to “Required” or “Invalid” for ALL of my model properties (to support the way the view presents the error messages). In retrospect, this probably wasn’t the smartest choice. Oh well, now I’m stuck figuring out another way to determine the properties causing Model validation to fail.

With Visual Studio 2015, I discovered a marvelous way to get the names of the properties with invalid values… buy using a watch! Watches now support lambda expressions (as long as there are no external dependencies, such as LINQ to SQL), so I thought… this would be a good time to test that functionality.

Since I was interested in the actual name of the property that was causing
ModelState.IsValid to return false, I used the following watch:

ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => x.Key).ToList()

For those who don’t usually use watches, you may not know that a watch can be created directly in the watch window by just typing it in! That is what you will need to do here, since I’m guessing that this value is most likely not in your code…

Untitled

If you get a message under Value saying “This expression causes side effects and will not be evaluated,” just click the refresh button to the right of the message to force-evaluate the watch. Once you do so, you’ll be able to expand the watch object to see your offenders!

Untitled

Pretty neat!

Advertisements

Javascript Intellisense in Visual Studio 2012

After years of use, I tend to accumulate a lot of cruft on my work computer hard drives, so whenever I replace a machine or hard drive I like to start fresh and only pull down old files from the backup server as I need them.  Last week, I replaced the hard drive on my laptop, so I had to redo all of the settings on Visual Studio.  As I had forgotten how to do this, I figured I should write it down to help me next time… perhaps it will help you too?

When developing Visual Studio MVC projects, if you’re like me, you like to move directories around to make more sense of the layout (instead of the default structure that M$ gives you).  An example of this is creating a public directory to hold your publicly available files, like styesheets and scripts.  You’ll also notice that when you move your Javascript libraries, Intellisense breaks.  This happens whenever the _references.js file is moved from its expected location*.

To fix, simply go to Tools | Options | Text Editor | JavaScript | IntelliSense References.  Switch to the Implicit (Web) Reference Group and add a new reference that resolves to the new location of the _references.js file:

VSJSOptions

Note that if you want to use a path relative to your project, it has to be entered into the text box at the bottom, in the form of “~/whatever_dirs_below_project_root”.

* If you want Intellisense support for your Javascript libraries, you need to be sure that the library name is included in the _references.js file.  This is not all, however.  VS also needs a vsdoc.js file that provides the particular data needed by Visual Studio Intellisense (e.g. jquery-1.10.0-vsdoc.js supports jquery-1.10.0.js).  Note that you should NOT include the vsdoc file name in the _references.js file, just the main file.  Intellisense vsdoc files for jquery are available on the ASP.NET CDN at http://www.asp.net/ajaxlibrary/cdn.ashx.

What is up with that %@#*&( batch file?!?!

So I’m working on a batch file to run some post-build events in Visual Studio, but when i run the build I notice something funny in the output window:

{{[[@ECHO OFF is not recognized as a valid command

Disclaimer: That might not be exactly how the error read, because I’ve already fixed the error and I’m too lazy to recreate it to get the exact wording, but you get the idea.

Now, the first line in my batch file is in fact a valid command, but there are all of these funny characters at the beginning of the line in the output window. So I go back to the actual batch file and those funny characters are not there.
So here I am scratching my head, thinking “I didn’t put those characters there, so who did?” Of course, my first reaction is to blame the machine!
After fiddling around with it, I discovered that whenever you create a new text file in Visual Studio and make it a batch file, it introduces these funny characters to the beginning of the file. I can only speculate (since I didn’t actually look at the bytes in the file), but I think it might be a Unicode vs ASCII encoding issue.

Long story short: if you want to create a batch file, first create it Notepad. Save it off and then edit it in Visual Studio and you won’t have any problems with encoding.