Search code examples
jqueryasp.net-mvcjquery-ui

How can I retrieve element Id for Strongly typed ASP.NET MVC HTML helper?


Possible Duplicate:
Client Id for Property (ASP.Net MVC)

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?


Solution

  • 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();