Part 44 Display and edit templated helpers in asp net mvc

preview_player
Показать описание
Note: Replace ] with GREATERTHAN and [ with LESSTHAN symbol

Templated helpers are introduced in mvc 2. These built in templated helpers can be broadly classified into 2 categories.
1. Display Templates
2. Editor Templates

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

There are 3 DISPLAY templated helpers
@Html.Display("EmployeeData") - Used with a view that is not strongly typed. For example, if you have stored data in ViewData, then

we can use this templated helper using the key that was used to store data in ViewData.
@Html.DisplayFor(model =] model) - Used with strongly typed views. If your model has properties that return complex objects, then

this templated helper is very useful.
@Html.DisplayForModel() - Used with strongly typed views. Walks thru each property, in the model to display the object.

Along the same lines, there are 3 EDIT templated helpers
@Html.Editor("EmployeeData")
@Html.EditorFor(model =] model)
@Html.EditorForModel()

To associate metadata with model class properties, we use attributes. In the previous sessions of this video series, we have

discussed about using various data annotations attributes. These templated helpers use metadata associated with the model to

render the user interface.

The built-in display and edit templated helpers can be very easily customised. We will discuss this in a later video session.

We will use the following Employee class that we have been working with in the previous sessions.
[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
}
public class EmployeeMetadata
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }

[ReadOnly(true)]
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }

[ScaffoldColumn(true)]
[DataType(DataType.Currency)]
public int? Salary { get; set; }

[DataType(DataType.Url)]
[UIHint("OpenInNewWindow")]
public string PersonalWebSite { get; set; }

[DisplayAttribute(Name = "Full Name")]
public string FullName { get; set; }

[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? HireDate { get; set; }

[DisplayFormat(NullDisplayText = "Gender not specified")]
public string Gender { get; set; }
}

Copy and paste the following Details action method in HomeController. Notice that, the employee object is stored in ViewData using

"EmployeeData" key.
public ActionResult Details(int id)
{
SampleDBContext db = new SampleDBContext();
Employee employee = db.Employees.Single(x =] x.Id == id);
ViewData["EmployeeData"] = employee;
return View();
}

At this point, if you run the application, you should be able to view Employee details, as expected.

Now, change the implementation of "Details" action method with in home controller as shown below. Notice that, instead of storing

the "Employee" object in ViewData, we are passing it to the View.
public ActionResult Details(int id)
{
SampleDBContext db = new SampleDBContext();
Employee employee = db.Employees.Single(x =] x.Id == id);
return View(employee);
}

You work with Editor templates in the same way. In HomeController, implement Edit action method as shown below.
public ActionResult Edit(int id)
{
SampleDBContext db = new SampleDBContext();
Employee employee = db.Employees.Single(x =] x.Id == id);
return View(employee);
}

Text version of the video

Slides

All ASP .NET MVC Text Articles

All ASP .NET MVC Slides

All Dot Net and SQL Server Tutorials in English
Рекомендации по теме
Комментарии
Автор

Hi, thanks for the video!
little thing.. not need to hit "build" button when you just changing something in the .cshtml file..

SergeiKjtydghk
Автор

Thank you so much for these fantastic videos!!

duel
Автор

I forgot to click Like button before entering my comment. I can rate this video as an excellent!!

krismaly
Автор

ohoo..good sir..i was not knowing this..

saagarsoni
Автор

This video show how to use Display Templates and Editor Templates. I see the amount of code reduced. I enjoyed watching this video and I recommend developers and others to watch. Is there any way you could make a video showing sane functionality of ASP Web form MVC form that explains how much code increased or decreased and why. I understand MVC is another version of ASP but it facilitates easy testing. Is there anyway could demonstrate in video? Thanks a bunch and good luck in your endeavors.

krismaly
Автор

Hi Venkat,
What is difference between & @Html.DisplayForModel(). They both produce same out put and are used for strongly typed views?

amolkolekar
Автор

Hi sir, could you give us a video about N-Tier solution, using the asp.net forms
i mean a solution with three tier : DAL "communicate to DB", BAL "between DAL and main project", and the main project..
if you dont have time could reference a good book, or video..
i wish to see it by your grate view..
Thanks always..

davidespada
Автор

Hi, Sir u have use meta data type partial class to customize the modal class but i did not understand how it work how it recognize that partial class or how it is realated..?

swapnilraut
Автор

After Watching ASP.NET MVC Series what should i have to learn next?
LINQ or Entity FrameWork

shahzaibriaz
Автор

how to create Employee class if Entity data model have Employee class  in Model

rampritsahani