Blog from Saravanan Arumugam

Let us talk about Technologies

Monthly Archives: January 2012

Friendly Names in MVC Html Helper created labels


Html helpers that come with MVC are smart enough to find the kind of field we try to work on and produce HTML accordingly. For instance, if I use the EditorFor() method

@Html.EditorFor()

for an integer or string, it’ll generate text box as the editor in the resulting HTML. If EditorFor() is applied on a Boolean, it’ll display a checkbox as the editor.

We have many helper methods that we can find very useful while writing MVC views. Most frequently used ones include,

@Html.EditorFor()
@Html.DisplayFor()
@Html.LabelFor()

Friendly Names

When we use LabelFor(), it would usually display the Property name (The variable name). For example, when I write the following, it simply displays the text “HasBranchesOverseas” on the Web page.

@Html.LabelFor(modelItem => item.HasBranchesOverseas)

On a web page we want the friendly names to be displayed not the variables names.

To provide a friendly name to the property, use System.ComponentModel. DisplayNameAttribute.

[DisplayName(“Has Branches Overseas”)]
public
 bool HasBranchesOverseas { get; set; }

 Now if you use @Html.LabelFor(), you’d see “Has Branches Overseas” on the web page, instead of the plain variable name.

The specified named connection is either not found in the configuration


I created a entity data model in one project and used it by referencing in a different project. .Net framework didn’t let me do this just by adding a project reference.

Exception

I got the following exception.

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

Solution

When I searched for a solution to this issue, I got a strange answer as the solution. Even though the project containing the entity model contains the named connection string in its app.config, it was required to copy it to the configuration file of the project referencing the entity. I had to manually copy the connection string from the app.config to the configuration of the web application where I used the entity.

Saving changes is not permitted


For my recent project, I was creating some tables using the SQL server designer. After creating tables, I tried to change the primary key to be of Identity column.

Exception

The change could not be saved and I received the following error message.

Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either…

image

Solution

It’s a defensive mechanism that SQL server offers us to prevent accidental loss of data due to unintentional changes. It is good to have this system on.

Following list of actions might require a recreation of the table, deleting the existing records.

  • Adding a new column to the middle of the table

  • Dropping a column

  • Changing column nullability

  • Changing the order of the columns

  • Changing the data type of a column

However, if you are sure enough that the change is intentional and you are aware the the table would be deleted and re-created, then you may proceed with the following step to override the defense system.

Select the Tools menu –> Options in the SQL server management studio.

In the Options dialog box, expand the Designers on the tree view and select Table and Designers. On the right hand side, uncheck the option, Prevent saving changes that require table re-creation.

image

Then select Ok. Now the designer should allow you to save the changes.

Again, be aware that when you save the changes, the entire table might be deleted and the existing records might get lost to accommodate the change.

Reference: Save (Not Permitted) Dialog Box

Changing titles in master page based web sites


We are aware that the <header> and <body> tags are contained in the Master page. Only the contents come in content pages.

One question that might arise is that how would the title change along with the change in content pages?

One workaround to do it is, provide a <ContentPlaceHolder> control inside the header of the master page. In the content page write the <title> tag.

Master page content would look like this.

<head runat=”server”>

<link href=”~/Styles/Site.css” rel=”stylesheet” type=”text/css” />

<asp:ContentPlaceHolder ID=”HeadContent” runat=”server”>

</asp:ContentPlaceHolder>

</head>

Content page would look like this.

<asp:Content ID=”HeaderContent” runat=”server” ContentPlaceHolderID=”HeadContent”>

<title>Title from the content page</title>

</asp:Content>

While running the application, you can see the “Title from the content page” in the content page.

Well, this is just a work around. There is a clear and straight solution provided by ASP.Net for this.

To change the title, use the title property of page directives (in the content pages).

<%@ Page Language=”C#” Title=”Title from the content page” MasterPageFile=”~/Site.master” AutoEventWireup=”true”

CodeBehind=”DataEntry.aspx.cs” Inherits=”MasterPageExploration._Default” %>

When the Asp.Net sees the title property, it renders it for your page.

Well, the next question.

What would happen if the <title> element is found in the master page itself? Is it legal to have it in the master/content page and provide the title element on the page directive?

Yes.

Here is the hierarchy of search, where the first holds the least priority and the last box holds the top priority.

image

However it is ideal/advised to keep the title element in the master page blank (or with a default site name), and provide the dynamic titles in each content page in the title property of the page directive.