views:

206

answers:

2
+2  Q: 

MVC2 Html Helpers

I generated a View using VS 2010 functionality. This auto code generation created this code:

<%: Html.TextBoxFor(model => model.DateDeleted, String.Format("{0:g}", Model.DateDeleted)) %>

I created a custom helper method (extension method) to create a JQuery Calendar control. This is the syntax for this control on the View:

<%= Html.DatePicker("Date", "../../content/images/calendar.gif", Model.DateDeleted)%>

How can I bind this to the data coming from my Controller? The Html.TextBoxFor allows a Lambda Expression in its signature. Here is the signature of the DatePicker code:

public static string DatePicker(this HtmlHelper helper, string name, string imageUrl, object date)

I am not sure how to wire the DatePicker with data coming from the Controller. Any help would be very much appreciated.

+3  A: 

Instead of using your own html helper use Html.EditorFor which allows you to use lambda expressions. Now prepare editor template with a jquery date piceker to use it as your editor. It will automaticaly display this template if you will put it in EditorTemplates directory. It's a standard in MVC2 and it really is a good practice.

This article shows how to make it! Enjoy!

ŁukaszW.pl
A: 

I have the exact solution on my blog. Use MVC template helpers to acheive this whilst being DRY.

http://www.dalsoft.co.uk/blog/index.php/2010/04/26/mvc-2-templates/

DalSoft