Part 46 Accessing model metadata from custom templated helpers

14
6



In this video, we will discuss, accessing model metadata from with in customized display and edit templated helpers. We will be using the same example, that we worked with in Part 45.

In Part 45, we have customized DateTime editor template to use jQuery calendar. The problem with this template is that, we have hardcoded the date format string to dd/MM/yyyy. So, from this point, any DateTime property that uses this template will format the date using the hard-coded date format string.

We want our DateTime template to be generic. We don’t want any hard-coded logic, with-in the DateTime editor template. The DisplayFormat attribute on the HireDate property of the Employee class must be used to determine the display format of the date.

Let’s remove the hard-coded date format string (dd/MM/yyyy) from DateTime.cshtml.
@Html.TextBox(“”, Model.HasValue ? Model.Value.ToString(“dd/MM/yyyy”) : “”, new { @class = “date” })

Decorate HireDate property in Employee class, with DisplayFormat attribute as shown below. Also, make sure ApplyFormatInEditMode parameter is set to true, otherwise the formatting will not be applied in Edit mode.
[DisplayFormat(DataFormatString = “{0:dd/MM/yyyy}”, ApplyFormatInEditMode=true)]
public DateTime? HireDate { get; set; }

Now, change the code in DateTime.cshtml to use the formatted datetime value as show below. Notice that, we are using ViewData.TemplateInfo.FormattedModelValue.
@Html.TextBox(“”, Model.HasValue ? ViewData.TemplateInfo.FormattedModelValue : “”, new { @class = “date” })

To access model metadata in templates, use @ViewData.ModelMetadata. For, example to access the DisplayFormatString, use @ViewData.ModelMetadata.DisplayFormatString. Along the same lines, if you want to know, the name of the containing class(i.e the class that contains the HireDate property) then use @ViewData.ModelMetadata.ContainerType.ToString().

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

All Dot Net and SQL Server Tutorials in Arabic

Nguồn: https://wijstaanvooronzegrondrechten.org

Xem thêm bài viết khác: https://wijstaanvooronzegrondrechten.org/cong-nghe

14 COMMENTS

  1. as much as I know the attribute applyformatineditmode is somethign only for google chrome, cuz in other browsers it will work well

  2. Hi Venkat.. One query on the date picker and template helpers. I couldn't grab the real benefit out of creating a class and adding date related attribute and calling that again in .cshtml. If we compare asp.net it was pretty much easy to add a date picker control on aspx page and any we can implement required validations in aspx page. If i need to change any thing here in this video i need to flip to c# class and change the property or attribute and need to compile and deploy when it comes to production, i feel its not that much easy or may be i am mssing some thing . Some how i feel asp.net was bit easy. There may be actual benefit which i am missing , can you please help me in understand

  3. And how to make datepicker set date AND time according to the textbox rules, because it seems that it's only affected by its own dateFormat, which doesn't include time at all.

  4. For some reason I am getting "HireDate must be a date" error on submitting the form if I select a date value having DayOfMonth more than 12. Seems like dd/MM/yyyy is casted as MM/dd/yyyy

    @kudvenkat Can you please help

  5. Thank you very much for taking time to give feedback. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

  6. sir i have one query about that, how to run our program at the startup on safe mode and normal mode as like when we startup our pc the anti virus get started automatically

    and how to run a program as service…..

  7. I enjoyed watching this short video and good to know formatting. I recommend developers to watch for learning.Thanks a lot

LEAVE A REPLY

Please enter your comment!
Please enter your name here