About the author

Brian Keating is a developer addicted to Microsoft Technologies.

Month List

RecentComments

Comment RSS

Asp MVC Entity Framework and Data annotations

clock February 28, 2011 23:09 by author Brian Keating |

 

image

 

When you create a MVC view for data entry it already has the scaffolding in place to handle client side validation (and server)
However you need to specify the data annotations you need.

You’ve two options.

1) Modify the T4 code generation templates

2) Use meta data classes (this post)

The generated EF classes are partial classes, this allows us to do the following

   1:  #region Feed
   2:   
   3:      [MetadataType(typeof(FeedMetadata))]
   4:      public partial class Feed
   5:      {
   6:      }
   7:   
   8:   
   9:      public class FeedMetadata
  10:      {
  11:          [Required(ErrorMessage="Please enter a valid name for this feed")]
  12:          public string Name { get; set; }
  13:      }
  14:   
  15:      #endregion Feeds

 

Note: while mvc3 supports these annotations they are limited to [range/required/stringlength/regularexpression]. If you wish to have a bigger offering consider the extensions offered.

Here’s how.

Go the NuGet Extension Manager (right click on references and select add Library Package Reference) and search for “DataAnnotationsExtensions.”  You should see the following two packages, choose the second (includes client side validation, first is server side only.)

Btw: If NuGet is alien to you, then don’t worry, it’s basically a packet manager to make your life a littler easier… a post for another day!




SqlServer 2008 Prevent Saving changes

clock February 27, 2011 22:56 by author Brian Keating |

 

When you modify a table in SqlServer and this change requires the table to be dropped and recreated, it’s necessary that SSMS is configured to allow this.

 

image

 

Go to SSMS/Tools/Options designers and uncheck this option.

See below

 

image




Create those delete views

clock February 27, 2011 20:06 by author Brian Keating |

 

You may wonder why you would create a view for the delete action

image

 

One reason is to prevent webcrawlers interfering with your data, so create those delete views!




Using Telerik RadScheduler with MVC3

clock February 23, 2011 23:24 by author Brian Keating |

 

The cool hip thing these days is MVC, hang ur head in shame if you are still plodding along with plain old asp.net.

Actually that’s not quite right, in fact it couldn’t be further from the truth, asp.net is alive and strong and will be for a long long time, don’t mind those people complaining about

  • Not having direct (easy) control over what html gets rendered
  • Testing lets a little to be desired
  • You can’t get ur grubby little hands on the @razor (at least i’m not currently aware if it can be used outside MVC3);

ASP is alive and strong and Microsoft are actively working on it, what’s more you’ve most likely already have some pretty good controls that you slaved over or bought ready made…

I’m a bit partial to Telerik controls and tonight I wanted to use the RadScheduler in a webportal i’m working on, and here it is in action

image

 

image

So how did I manage to do this?

Well I started out with a HtmlHelper extension and then realized it was even easier.

Telerik RadScheduler works directly with webservices (please read their documentation for full info (because my posts are more pointers then full working samples)

The View contains the following (apologies in all my talk I didn’t use razor Embarrassed smile)

   1:  <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
   2:  <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
   3:  <asp:Content ID="HeaderCnt" ContentPlaceHolderID="TitleContent" runat="server">
   4:      
   5:  </asp:Content>
   6:   
   7:  <script runat="server">
   8:      public override void VerifyRenderingInServerForm(Control control)
   9:      {
  10:      
  11:      }
  12:  </script>
  13:  <asp:Content ID="BdyCnt" ContentPlaceHolderID="MainContent" runat="server">
  14:      <h2>Index</h2>
  15:      <telerik:RadScheduler runat="server" ID="SampleRadScheduler" EnableAdvancedForm="false">
  16:          <WebServiceSettings Path="~/Models/FeedWebService.asmx" />
  17:          <AdvancedForm Modal="false" />
  18:      </telerik:RadScheduler>
  19:  </asp:Content>

 

That’s pretty muich it, I’m using a plain old .net 2.0 style Webservice as outlined in Telerik help samples but expect I can use WCF too (another post maybe).

 

enjoy..




SqlDatasource Strict Concurrency Checking–Simplistic approach

clock February 23, 2011 14:34 by author Brian Keating |

 

Lets say you have simple UPDATE command

UpdateCommand="Update SEC_USERS SET FirstName=@FirstName, Department = @Department, Gender = @Gender”
 
 
 

Now, lets assume that two end users of your system have already requested information for SEC_USER Bill whose a male janitor.

End user 1 updates Bills Department as he’s been promoted to CEO (hey it not impossible!).
End user 2 changes Bills Gender to female.

What actually ends up happening is the End user 2 overwrites the promotion and Bill (or maybe billie now ) ends up as a female janitor.

So how can we solve this problem assuming we are using a SqlDataSource.

Here’s how:

image

  • Set the ConflictDetection property to CompareAllValues
  • Set the OldValuesParameterFormatString to xxxx_{0}
  • Update your command with a WHERE restriction for XXXX_{0}
    UpdateCommand= "Update SEC_USERS SET FirstName=@FirstName, Department = @Department, Gender = @Gender FROM SEC_USERS WHERE FirstName=@read_FirstName
     AND Department=@read_Department AND Gender=@read_Gender"

That’s it, the SqlDataSource manages the read_xxx parameters so you don’t have to.