views:

15

answers:

1

So here's a DateTime editor a la NerdDinner's /Views/Shared/EditorTemplates/DateTime.ascx:

%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime>" %>
<%: Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model))%>
<script type="text/javascript">
    $(function () {
        $("#date").datepicker({ showOn: 'focus' });
    });
</script>

Since this is a template that's going to be used possibly multiple times on a page, if I'm going to attach the datepicker to each TextBox, they each need a unique ID, and I need to be able to use them in the <script>.

What's the best way to go about doing this?

+1  A: 

I'm not much of an MVC programmer, but surely you can just use a class instead of an ID somehow?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime>" %>
<%: Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model, new { @class = "datepicker" }))%>
<script type="text/javascript">
    $(function () {
        $(".datepicker").datepicker({ showOn: 'focus' });
    });
</script>
Codesleuth
Awesome... thanks!Though I'm still baffled as to how that managed to work -- how does it know which TextBox I'm talking about if they both have the same name? Is it just because I have them scoped out in different <divs>?
Rei Miyasaka
`new { @class = "datepicker" }` forces the class on to the textbox (view the source, you should be able to see it) and `$(".datepicker").datepicker` applies to all matching elements, which in this case is all the textboxes. jQuery is awesome, I can't say that enough :)
Codesleuth