In the last blog post, we discussed about
ScaffoldColumn attributes in ASP.NET MVC. You can read that article here. In this article, we will continue with
Let’s understand these with an example. I am taking the same example which we have used in the previous article. There, we have seen that while running the application, the email address is showing as a plain text.
Instead, we want the email address to be rendered with mailto hyperlink. The advantage of this is that when we click on that hyperlink, it is going to open the default email address of the client automatically.
To achieve this, you simply need to decorate the
EmailAddress property within the
EmployeeMetaData class with
Let’s run the application. We can now see that Email Address is rendered with a mailto hyperlink.
Another scenario where you can use
DataType attribute is when you want to associate currency symbols. Look at the page, at the moment
Salary is displayed without any symbols. We want to associate a currency symbol with the
Salary. Let’s use
DataType attribute again for this purpose.
Let’s build our solution by pressing Ctrl+Shift+B and refresh the View. We can see that now
Salary is displayed with a
$ symbol by default in my machine.
But instead, we want to associate some other currency symbol, say Indian Rupees with this amount. To achieve this, in Web.config file, specify
Save the changes and refresh the View. We can now see a Rupees symbol instead of a $ symbol.
Now look at the Personal Web Site. At the moment, it is displaying as a plain text. Instead, we want that has to be displayed as a hyperlink. We can achieve this very easily. One simple thing you have to do is decorate that property with
Build the solution and refresh the View, we can see that now Personal Web Site is rendered as a hyperlink. While clicking on the hyperlink, you will be navigated to this blog.
Now let’s look at the use of
DisplayColumn attribute is useful, when a class has a property of complex type and you want to pick one property of this complex object for display purpose. Let’s understand this with an example.
First of all, right click on the Models folder and add Company.cs class file.
Copy and paste the following code:
public class Company
public tblEmployee CompanyDirector
SampleDBContext db = new SampleDBContext();
return db.tblEmployees.Single(x => x.Id == 1);
Notice that this class has
CompanyDirector property which returns a
tblEmployee is a complex type.
tblEmployee object has got several properties. If you want
FullName to be used for display purpose, then make the following changes.
tblEmployee partial class in Models folder, with
Details action method in Home controller as shown below.
public ActionResult Details(int id)
Company company = new Company();
Copy and paste the following code in Details.cshtml view.
ViewBag.Title = “Details”;
@Html.DisplayTextFor(x => x.CompanyDirector)
Now run the application and navigate to localhost:57506/MVCDemo/Home/Details/3. You should see the Full Name of the employee.