Tuesday, 20 September 2011
Change the default Check-in Action of Visual Studio 2010
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\TeamFoundation\SourceControl\Behavior
change @ResolveAsDefaultCheckinAction to 'False'
from Stack Overflow answer
Wednesday, 31 March 2010
ObjectQuery Include Extension
contexte.Users.Include("Roles")So I decided to write an extension that would enable to use an expression likecontexte.Users.Include(u => u.Roles)so here is the code:
public static class ObjectQueryExtensionsand you also need this:
{
public static ObjectQuery<TEntity> Include<TEntity, TProperty>(this ObjectQuery<TEntity> query, Expression<Func<TEntity, TProperty>> expression) where TEntity : class
{
string name = expression.GetPropertyName();
return query.Include(name);
}
}
public static class ExpressionExtensions
{
public static string GetPropertyName<TObject, TProperty>(this Expression<Func<TObject, TProperty>> expression) where TObject : class
{
if (expression.Body.NodeType == ExpressionType.Call)
{
MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
string name = ExpressionExtensions.GetPropertyName(methodCallExpression);
return name.Substring(expression.Parameters[0].Name.Length + 1);
}
return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
}
private static string GetPropertyName(MethodCallExpression expression)
{
MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
if (methodCallExpression != null)
{
return GetPropertyName(methodCallExpression);
}
return expression.Object.ToString();
}
}
Monday, 8 February 2010
DisplayNameAttribute Localized
So here is a simple Attribute, extending the DisplayNameAttribute, inspired from a post on stackoverflow
using System;
using System.Linq;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
namespace MvcExtensions.Attributes
{
[AttributeUsage(AttributeTargets.Property AttributeTargets.Field, AllowMultiple = false)]
public class DisplayNameLocalizedAttribute : DisplayNameAttribute
{
public DisplayNameLocalizedAttribute(Type resourceType, string resourceKey)
: base(LookupResource(resourceType, resourceKey)) { }
internal static string LookupResource(Type resourceType, string resourceKey)
{
PropertyInfo property = resourceType.GetProperties().FirstOrDefault(p => p.PropertyType == typeof(System.Resources.ResourceManager));
if (property != null)
{
return ((ResourceManager)property.GetValue(null, null)).GetString(resourceKey);
}
return resourceKey;
}
}
}
An I use it simply like this:
using MvcExtensions.Attributes;
using MyMvc2Rc2TestApp.App_LocalResources;
namespace MyTestMVC2App.Areas.UserManagement.Models
{
public class User
{
[DisplayNameLocalized(typeof(DisplayName), "User_UserName")]
public string UserName { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_Password")]
public string Password { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_FirstName")]
public string FirstName { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_LastName")]
public string LastName { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_Email")]
public string Email { get; set; }
}
}
I have a DisplayName.resx file in App_LocalResources, it is an Embedded Resource with Public Access Modifier
Asp.Net MVC Manager for javascript include and Css files
I was trying to figure a way to automatically include javascript from html helpers and from view without having to check if it wasn't already in the master so i came up with a simple manager. It is greatly inspired by Frugal Coder Blog and Chris Pietschmann. I build an abstarct manager to perform the operation required by the javascript and css manager.
so here it is :
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
namespace MvcExtensions
{
public abstract class MvcManager
{
protected HtmlHelper _htmlHelper;
protected Dictionary<string, TagBuilder> _include = new Dictionary<string, TagBuilder>();
public MvcManager(HtmlHelper htmlHelper)
{
this._htmlHelper = htmlHelper;
}
public virtual MvcManager Include(string path)
{
if (!this._include.ContainsKey(path))
{
this._include.Add(path, this.BuildTag(UrlHelper.GenerateContentUrl(path, this._htmlHelper.ViewContext.HttpContext)));
}
return this;
}
public virtual MvcHtmlString Render()
{
StringBuilder sb = new StringBuilder();
foreach (var item in this._include.Values)
{
sb.AppendLine(item.ToString());
}
return MvcHtmlString.Create(sb.ToString());
}
protected abstract TagBuilder BuildTag(string src);
}
}
And now for the javascript manager:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace MvcExtensions
{
public class MvcClientScriptManager : MvcManager
{
private List<string> _readyLine = new List<string>();
public MvcClientScriptManager(HtmlHelper htmlHelper) : base(htmlHelper) { }
public new MvcClientScriptManager Include(string path)
{
return (MvcClientScriptManager)((MvcManager)this).Include(path);
}
public MvcClientScriptManager Include(string path, string debugPath)
{
string file = this._htmlHelper.ViewContext.HttpContext.IsDebuggingEnabled ? debugPath : path;
return this.Include(file);
}
public MvcClientScriptManager AddToReady(string code)
{
this._readyLine.Add(code);
return this;
}
protected override TagBuilder BuildTag(string src)
{
TagBuilder scriptTag = new TagBuilder("script");
scriptTag.MergeAttribute("type", "text/javascript");
scriptTag.MergeAttribute("src", src);
return scriptTag;
}
public override MvcHtmlString Render()
{
if (this._readyLine.Any())
{
StringBuilder sb = new StringBuilder(base.Render().ToString());
sb.AppendLine("<script type=\"text/javascript\">");
sb.AppendLine("$(function() {");
foreach (var item in this._readyLine)
{
sb.AppendLine("\t" + item);
}
sb.AppendLine("});");
sb.AppendLine("</script>");
return MvcHtmlString.Create(sb.ToString());
}
return base.Render();
}
}
}
The CssManager :
using System.Web.Mvc;
namespace MvcExtensions
{
public class MvcCssManager : MvcManager
{
public MvcCssManager(HtmlHelper htmlHelper) : base(htmlHelper) { }
public new MvcCssManager Include(string path)
{
return (MvcCssManager)base.Include(path);
}
protected override TagBuilder BuildTag(string src)
{
TagBuilder linkTag = new TagBuilder("link");
linkTag.MergeAttribute("type", "text/css");
linkTag.MergeAttribute("rel", "stylesheet");
linkTag.MergeAttribute("href", UrlHelper.GenerateContentUrl(src, this._htmlHelper.ViewContext.HttpContext));
return linkTag;
}
}
}
my master page now looks like this:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<% Html.MvcCssManager().Include("~/Content/Css/Site.css"); %>
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
<%= Html.MvcCssManager().Render()%>
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>My MVC Application</h1>
</div>
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
<div id="menucontainer">
<ul id="menu">
<li><%= Html.ActionLink("Home", "Index", "Home")%></li>
<li><%= Html.ActionLink("About", "About", "Home")%></li>
<li><%= Html.ActionLink("Users", "List", "UserManagement")%></li>
</ul>
</div>
</div>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
</div>
<%= Html.MvcClientScriptManager().Render() %>
</body>
</html>
And finally a sample view in which I use it:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyTestMVC2App.Areas.UserManagement.Models.User>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
<%
Html.MvcClientScriptManager()
.Include("~/Scripts/MicrosoftAjax.js", "~/Scripts/MicrosoftAjax.debug.js")
.Include("~/Scripts/MicrosoftMvcValidation.js", "~/Scripts/MicrosoftMvcValidation.debug.js");
Html.MvcCssManager().Include("~/Content/Css/Jquery.UI/base/ui.all.css");
%>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<%= Html.EditorForModel() %>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "List") %>
</div>
</asp:Content>
So now I'm only referencing script and css hosted on my site but thats for another post...
Feel free to leave comment, I would love to have some feed-back on this.
Saturday, 21 March 2009
Detecting errors in ASP.NET MVC views
this is a copy from ASP.NET MVC - RC Release Notes.
ASP.NET Compiler Post-Build Step
Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the project file and set the MvcBuildViews property to true, as shown in the following example:<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup><MvcBuildViews>true</MvcBuildViews></PropertyGroup>
Note Enabling this feature adds some overhead to the build time.
You can update projects that were created with previous releases of MVC to include build-time validation of views by performing the following steps:
1. Open the project file in a text editor.
2. Add the following element under the top-most <propertygroup> element:<MvcBuildViews>true</MvcBuildViews>
3. At the end of the project file, uncomment the <target name="AfterBuild">element and modify it to match the following example:<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'"><AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" /></Target>
Tuesday, 17 March 2009
Modal div with JQuery
Click for demo
Here are some code sample :
Simple call :
$('#modalContentTest').modal();With callbacks :
$(this).modal({
onOpen: OnModalOpen,
onClose: OnModalClose,
persist: true
});
How to put a div in the center of the screen using CSS
<html>
<head>
<title>DIV CENTER SCREEN</title>
<style type="text/css">
<!--
html, body
{
font-family: Arial, Helvetica, Sans-Serif;
padding:0;
margin: 0;
text-align:center;
height: 100%;
}
div#shim
{
visibility: hidden;
width: 100%;
height: 50%;
margin-top: -275px;
float: left;
}
div#wrapper
{
width: 650px;
height: 550px;
margin:0px auto;
text-align:left;
clear: both;
top: -275px;
position: static;
background-color: #7B9EbD;
text-align: center;
}
p
{
line-height: 550px;
font-weight: bold;
font-size: 16pt;
}
-->
</style>
</head>
<body>
<div id="shim"></div>
<div id="wrapper">
<p>
This div is in the center of the screen...
</p>
</div>
</body>
</html>
The trick is to put another div above the one you need centered, put it 50% height and a margin-top equal to half your centered div height. then put your centered div with a top of half it's height.
DIV Layout
I Lately had to build the layout for a website, this is a simple layout, and the picture represents what I want it to look. The challenge is to have a layout that would fill the width of the screen while having a fixed width left column. So here is my solution :
<html>
<head>
<style>
html, body
{
padding:0;
margin: 0;
width: 100%;
height: 100%;
}
div#page
{
width: 100%;
height: 100%;
}
div#header
{
background-color: #FF0000;
height: 80px;
}
div#menu
{
float: left;
width: 150px;
background-color: #00FF00;
}
div#pagecontent {
margin-left: 150px;
background-color: #0000FF;
}
div#footer
{
background-color: #636363;
text-align: center;
}
</style>
</head>
<body>
<div id="page">
<div id="header">
<h1>This is my title</h1>
<h2>This is my subtitle</h2>
</div>
<div id="menu">
<ul>
<li>this</li>
<li>is</li>
<li>my</li>
<li>left</li>
<li>menu</li>
<li>this</li>
<li>is</li>
<li>my</li>
<li>left</li>
<li>menu</li>
<li>this</li>
<li>is</li>
<li>my</li>
<li>left</li>
<li>menu</li>
</ul>
</div>
<div id="pagecontent">
This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page. This is the content onf my page.
</div>
<div id="footer">
this is my footer
</div>
</div>
<BODY>
</html>
Sunday, 8 March 2009
ASP.NET MVC must see
Here is a list of link on the subject
- ASP.NET MVC : The Official ASP.NET MVC Site
- ASP.NET MVC - ASP.NET Forums
- Stephen Walther on ASP.NET MVC
- ASP.NET MVC Framework on Wikipedia
- ASP.NET MVC on CodePlex
And here are some intersting Blod feed
- ASP.NET News
- Latest Microsoft Blogs
- ASP.NET Daily Articles
- ASP.NET MVC Tutorials
- Stephen Walther on ASP.NET MVC
Later...