Tag Archives: postaweek2011

MVC3 and Telerik Grids

The Background (feel free to skip it)

As we all know in MS change is a slow process. However something is definitely wrong (in a good sense) with the ASP.NET team. They seem to be innovating at the speed of thought!

MVC is already into revision 3 and the PDC demos by Scott Hanselman are already outdated (they phased out MvcScaffold replaced it with MvcScaffolding package).

Either-ways MVC + Scaffolding + EF seemed like a very nice way to re-do the archaic 4 year old system I am currently maintaining. Whether someone needs it at work or not it is a good ‘real-world’ (as opposed to ‘hello, World’) project to take up, while learning ASP.NET MVC.

But the grid implementations out of the box is pathetic and I stumbled upon Telerik’s feature packed MVC Extensions and guess what, they have a GNU license version for you to play around with. It was even available for download through Nuget package manager. Wow!

Anything that’s too good to be true usually is…

My joy was short lived. Off the bat the new scaffolding generator refused to use the custom t4 templates (like Scott had shown). I fiddled around a little but couldn’t figure it out so left it and changed the view by hand to convert the Index page such that it uses the Telerik Grid.

Examples seemed helpful enough and slowly but surely things were falling into place when I noticed the filtering functionality was not working! Then I noticed the Popup for New/Edit was not centered and did not have correct styling (background wasn’t disabled and popup-header’s drag wasn’t working). Started digging around. View Source indicated some javascript files were missing. Added them, and BAM! Hell broke loose, jScript errors all over the place. Everything stopped working.

Retrace Steps (abandon brute force)

Registered and downloaded the latest samples from Telerik (the project had the package installed through Nuget package manager). The sample ran like a champ in local environment. Confused again!

  • Added all the latest javascripts from sample to my project
  • Added the Telerik.Web.Mvc from sample to my project (essentially replaced the reference added by Nuget)
  • Then I noticed the following little piece missing in the _Layout.cshtml

image

Even after adding it the errors continued.

Back to sample.

  • Then I noticed in the sample _Layout.chtml there were no direct references to the individual js files (from telerik). All it had was the above registration code. Light bulb moment!!!
  • Removed the direct references to the other js files and kept the registration code only. Still no luck! But this time I got an exception in the above line saying one of the js files not found under “~/Scripts/” folder. Oh Boy! All the telerik js files need to be under “~/Scripts/”. Moved them out to the  ‘Scripts’ root and changed the code to look like

image

  • Eureka moment! Finally things look the way they should.

In Conclusion

  • Telerik extensions available through Nuget (today) are not the latest (they use jquery 1.4.3 as opposed to MVC 3 using 1.4.4)
  • Download latest from Telerik.
  • You only need to register the above javascript and the rest of the scripts are registered automatically
  • All the Telerik javascripts need to be in the “~/Scripts/” folder. Don’t be fooled by the cryptic folder (like 2010.3.1110) created by Nuget. You will need them to be in the root folder.
  • Don’t rely on ‘View Source’ completely. There is still some amount of backend munging going on.
  • Unlike me start with the sample and move it to your project, not the other way round.
  • Stay tuned for T4 templates to generate views and controllers taking advantage of Telerik extensions.
  • Last but not least Telerik MVC extensions at-least for the Grid are pretty powerful and I thank Telerik for making the entire suite available under GNU license.
Advertisements
Tagged , , , , , , ,

How to: Use default ASP.NET Authentication and Authorization Provider Schema in your own Entity Model?

Background

Microsoft has provided a default authentication provider that stores usernames and passwords in a SQL Server Database. The schema and the provider come out of the box and now-a-days if you use and Web application template, Visual studio will generate all code necessary to do forms authentication and registration.

The default database for storing Authentication and Authorization mechanism is called aspnetdb and contains all the tables and stored procedures required.

The Problem

You can choose to keep the default aspnetdb and build your application schema separately and use the provider as needed. This works fine when your own schema elements don’t need to access the roles, user or membership entities that are getting stored in aspnetdb. But we run into trouble if we are building our schema using the Entity Framework and want to use entities from the aspnetdb. Entity Designer can connect to only one database at a time. So how do we keep the out-of-the-box goodness intact, use the Entity Framework and also use the Role/membership entities in our custom entities?

Solution

Microsoft has provided a tool for generating the entire schema in any database you want. The details are available at http://msdn.microsoft.com/en-us/library/ms229862.aspx

To Summarize

1. Open “Visual Studio Command Prompt” from “Visual Studio Tools” shortcut.

2. Type in the following commands

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>aspnet_regsql –d [YourDataBaseSchemaName] –A all –E –S [SQLSERVER NAME]

The aspnet_regsql tool has a lot of options, details for which are well documented by MS in the above link. Above I’ve used three options

-d : Implies you are specifying a database schema where the scripts will be generated

[YourDataBaseSchemaName] : Replace this with the schema/database name with the one for your application

-A : This implies Add

all : This value implies you want all the tables in your schema. There are some options to get a subset, but I just went for the whole thing

-E : This implies you are asking it to Authenticate using current Windows Authentication for current user.

-S: SQL Server Name. It is usually the machine name or [machine name]\SQLSERVER unless you’ve named it differently yourself.

This script hardly takes a second to execute.

3. Now the authentication infrastructure is in place. Few more steps to go:

  • Change default connection string in your web.config for the provider. By default the connection string is called “ApplicationServices”. Change it such that it connects to your database.
  • Open the Entity Designer, right click on the design surface and select Update Model from Database.
  • Select all the tables (only) and add them to the Entity Design surface.
  • Rename the aspnet_* tables as per your naming conventions. Renaming the entities doesn’t change the table names in the DB so rest assured you are not breaking anything with the default provider.

That’s it. Now you have access to the entities from the providers in your schema, feel free to use them they way you want. The default provider is intact and you get all the Entity Framework goodies for reading from the provider free of custom data layer development cost.

Have Fun!

Tagged , , , , , ,

The Blog a Week Challenge!

At beginning of the year, WordPress was kind enough to send me a report of my Blog’s status. The saddest part of the report was the fact that I had blogged a total of 4 times in the year. It took a while to digest and actually before I could digest it WordPress threw the Blog a Day and Blog a week challenge at all it’s bloggers.

I won’t contemplate the blog a day challenge considering I find it hard to ‘tweet a day’, but I told myself I could blog once a week. Not could I HAD to blog once a week.

Well the first month is almost over and unless I put up three more articles in the next few hours I am –3 in the first month of my blog a week challenge too! That is a shame. But I am not giving up. Proof is this filler article that’s nothing but a personal rant, a public acceptance of the challenge, it might just spur me to do better!

BTW my first article of the year wasn’t on my blog but on dotnetcurry.com. It’s a Sharepoint article. I’ve written two on dotnetcurry so far. The third one is in the works.

Tagged , ,
%d bloggers like this: