views:

34

answers:

1

Hi In my View I'm using jquery ui datapicker. So I need initiate it with code like this


$(function() {
        $('#elementID').datepicker({
        });
});
    

In my View


     <%= Html.TextBoxFor(m=>m.StartDate) %>
    

In old ASP.NET I may use


    tb_startDate.ClientID
    

What is about retrieving element Id of Strongly Typed ASP.NET MVC HTML Helper? Is is possible?

+2  A: 

There is no way of receiving the id of the textbox once it has been rendered (as it just outputs plain text).

You can, however use another approach where you set the class and use a standard ".class-jquery selector".

Like so:

<%= Html.TextBoxFor(m=>m.StartDate, new { @class = "startDate" }) %>

and:

$('input.startDate').datepicker();
Mickel
10x for your advise, but I'm thinking(and googling) about another solution. Yours is not a good idea in terms of performance.
sh1ng
I'm not sure the proposed solution has any issues with regards to performance at all. I'd be interested to see a link that suggested otherwise :)
Amadiere
http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2
sh1ng
Sure, selecting by ids is faster. But for just 1 to 100 selectors on one page, I wouldn't bother implementing a custom helper just to get the Id as it is not that much faster (we're talking milliseconds).
Mickel