参考与TextBoxFor创建控件的ID() [英] Reference a control's ID created with TextBoxFor()
问题描述
我的爱的ASP.NET MVC,紧跟发布/文档有时会非常棘手,所以也许我只是没有得到的东西......我想用一个TextBoxFor() ,并与LabelFor()等工作是好的,所有的魔法发生了我。
I am loving ASP.NET MVC, keeping up with the releases/docs can sometimes be tricky, so maybe I'm just not getting something... I want to use a TextBoxFor(), and working with LabelFor() etc. is fine, all the magic happens for me.
但是,如果我创造... ...
But if I create...
<%=Html.TextBoxFor(x => x.LastName) %>
和想做一些与jQuery不错,我怎么会得到所创建的控件的ID?我可以添加一个CSS类,并用它来附上我的jQuery,但东西我做的,我想的ID ......所以我可以做这样的事情:
And wanted to do something nice with jQuery, how would I get the ID of the control that was created? I could add a CSS class and use that to attach my jQuery, but for something I am doing I would like the ID... so I could do something like:
$('#LastName').(...)
我知道我可以工作了在这种情况下,并且在手动攻击它,但有一个更合适的方法?
I know I could work it out in this case, and hack it in manually, but is there a neater way?
推荐答案
由于MVC4有一个内置的方式做到这一点 - 的 @ Html.IdFor()。
Since MVC4 there is a built-in way to do it - @Html.IdFor().
<一个href=\"http://www.nickriggs.com/posts/getting-the-id-and-name-attribute-generated-by-typed-html-helpers/\">Here是使用它的样本:
@Html.IdFor(m => m.Filters.Occurred.From)
和结果是像
Filters_Occurred_From
这篇关于参考与TextBoxFor创建控件的ID()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!