Using of Templated Helpers in MVC 2.0 : How can use the name of the property that I'm rendering insi
- by Andrey Tagaew
Hi.
I'm reviewing new features of ASP.NET MVC 2.0.
During the review i found really interesting using Templated Helpers.
As they described it, the primary reason of using them is to provide common way of how some datatypes should be rendered.
Now i want to use this way in my project for DateTime datatype
My project was written for the MVC 1.0 so generating of editbox is looking like this:
<%= Html.TextBox("BirthDate", Model.BirthDate, new { maxlength = 10, size = 10, @class = "BirthDate-date" })%>
<script type="text/javascript">
$(document).ready(function() {
$(".BirthDate-date").datepicker({ showOn: 'button', buttonImage: '<%=Url.Content("~/images/i_calendar.gif") %>', buttonImageOnly: true });
});
</script>
Now i want to use Template Helper, so i want to have above code once i type next sentence:
<%=Html.EditorFor(f=>f.BirthDate) %>
According to the manual I create DataTime.ascx partial view inside Shared/EditorTemplates folder.
I put there above code and stacked with the problem.
How can i pass the name of the property that I'm rendering with template helper?
As you can see from my example, i really need it, since I'm using the name of the property to specify data value and parameter name that will be send during the POST requsest. Also, I'm using it to generate class name for JS calendar building.
I tried to remove my partial class for template helper and made MVC to generate its default behavior.
Here what it generated for me:
<input type="text" value="04/29/2010" name="LoanApplicationDays" id="LoanApplicationDays" class="text-box single-line">
As you can see, it used the name of the property for "name" and "id" attributes.
This example let me to presume that Template Helper knows about the name of the property.
So, there should be some way of how to use it in custom implementation.
Thanks for your help!