Monday
Aug312009

Share your Visual Studio code examples on your blog

Jim's article How to Post Code From Visual Studio to Your Blog provides a very easy and effective way to add code examples from Visual Studio to your blog. Jim's article focuses on the blogging platform WordPress but it will work just as well in any blogging platform as your Visual Studio code is converted to HTML. As you can see it works great here in our Squarespace blog.

 

   17         //
   18         // GET: /ParentTags/
   19 
   20         public ActionResult Index()
   21         {
   22             return View();
   23         }
Wednesday
Aug262009

jQuery Tag Suggestion in ASP.NET MVC

On a ASP.NET MVC project I am currently working on I have implemented Remy's jQuery Tag Suggestion
plug in which works perfectly and makes a nice experience for people using my application.

I wanted to share a couple of learnings for others who would like to implement this plugin in their ASP.NET MVC applications where they need to return data from a database instead of a static tag list/file.

To make it easier for you to implement a similar function I have created an ASP.NET MVC project which can be downloaded here Tag Suggestion ASP.NET MVC project files and Tag Suggestion database back up. You can also check out a working version of this source code here ASP.NET Tag Suggestion demo

Of course the first step is to download the tag.js file from Remy's article and add this file to your Scripts folder in your MVC project.

Then you will need to add a reference to this file and add the tag script under the Head html tag in your Site.Master file normally found under the Views/Shared folder.

The reference to the tag.js file will look something like

   12     <script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" type="text/javascript"></script>

   13

   14     <script src="<%= Url.Content("~/Scripts/tag.js") %>" type="text/javascript"></script>

   15

   16     <% if (false)

   17        { %>

   18

   19     <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript" </script>

   20

   21     <script src="../../Scripts/tag.js" type="text/javascript"></script>

   22

   23     <% } %>

 

and the tag script for the Ajax version of the Tag Suggestion which will look up and return data from your database. The script should look like

   25     <script type="text/javascript">

   26

   27         $(function() {

   28             $('#ParentTags').tagSuggest({

   29                 url: 'TagSuggestion',

   30                 delay: 250

   31             });

   32         });

   33

   34     </script>

 

Notice the #ParentTags which will be the id of the textbox you would like to use for the Tag Suggestion feature. This textbox will call/fire the jQuery function. Also notice the url: 'TagSuggestion' which points to a action within the current controller file being used to return the view where you will be using this tag suggestion textbox.

Now you need to add the textbox to the view where you want to use the Tag Suggestion like this

   19 <%= Html.TextBox("ParentTags")%>

 

As you can see this text box has a name/id of "ParentTags" which will link to the script also called #ParentTags. To make it easy and clean I have also got a field in my database called "ParentTags" so I can save the tags the person enters.

Last but not least we need to add the Action to the Controller which will be used to render the view we added the textbox to above.

The Action code will look like

   82         public ActionResult TagSuggestion()

   83         {

   84

   85             var tagDetailsGrp = from tdg in db.vw_TagDetails_Grps

   86                                 orderby tdg.DetailIDCount descending

   87                                 select tdg;

   88

   89             ArrayList list = new ArrayList();

   90

   91             foreach (vw_TagDetails_Grp item in tagDetailsGrp)

   92             {

   93                 list.Add(item.DetailTag);

   94             }

   95

   96             return this.Json(list);

   97

   98         }

 

This Action code goes to a view in my database which groups all of the tags from the table called TagDetails and returns this list of tags. The code then adds each tag to an ArrayList which is then returned to the jQuery function in JSON format.

That's it now you should have your new jQuery Tag Suggestion function up and running. I hope this helps to save you a bit of time and feel free to let me know if you have any questions.

Tuesday
Aug252009

Linq to SQL delete multiple/bulk records (batch delete)

Normally when building ASP.NET applications Linq to SQL makes the job a whole lot easier. It takes care of all the data access for creating, updating and deleting individual records. However every so often I come across the need to delete multiple records such as the case with a one to many relationship, where I need to delete all related records for a specific group. Unfortunately this is not as simple as you would think using Linq to SQL as Linq to SQL only deals with one record at a time. For a large high performance application where large numbers of records need to be deleted the best approach would be to use a stored procedure. In some cases the delete is contained to small datasets and would not be used frequently so keeping all of your code in Linq to SQL keeps it clean and consistent. In this case I have found the sample code below does the trick.

MyAppDataContext db = new MyAppDataContext();

var deleteRelatedRecords =
from relatedRecords in db.RelatedRecords
where relatedRecords.MyForeignKeyID == MyPrimaryKeyID
select relatedRecords;

foreach (var relatedRecords in deleteRelatedRecords)
{
db.RelatedRecords.DeleteOnSubmit(RelatedRecords);
}

db.SubmitChanges();

This code works well if all you need to do is clean up/delete a small group of records, it first gets the group of records, then loops through the dataset flagging each one to be deleted. Once completed the changes are submitted committing the deletes.

Additional information and samples in other languages can be found here http://msdn.microsoft.com/en-us/library/bb386925.aspx

Friday
Jul102009

Award winning application

Congratulations! kmsystems client, IAG winners of the Australian Business Awards – 2009 for the category of innovation based on an application we built for them. We are very proud to have been able to be apart of their innovation project, such a great learning experience for us to apply our web 2.0 social media knowledge on. The end result an award winning application that has already produced significant dollar savings, savings on stationary paper such as manual forms and process steps, it doesn't get better than that.

Tuesday
Jun302009

Preview: ASP.NET MVC conversion of Whos Doin Wot

As I mentioned in my previous post ASP.NET MVC Membership Management I decided to port over a simple team management app we built called "Whos Doin Wot" from the Morfik RIA platform to ASP.NET MVC. I found the exercise to be a nice little training exercise and the chance to understand and play around with MVC. Also as a side benefit I have created some basic controllers and views for handling users and roles within ASP.NET membership (download available on the previous post detailed above).

I've spent a couple of days on it now and have a preview of the new MVC version up and running click here to have a go.

Here are a few screen shots as well:

Dashboard: is a clean and simple list of what the team are up to today, tomorrow, for the rest of the week and month.

 

Groups: here you have the ability to set up different groups which you can then add people/users to.

 

Add people: You have the ability to set up access for other team/group members so that they can access and contribute to specific groups

 

Create a new entry: Here you can quickly add in entries to let the team know what you are up to for today, tomorrow, the week and month (if you like to plan ahead).