客户端ID物业(ASP.Net MVC) [英] Client Id for Property (ASP.Net MVC)

查看:167
本文介绍了客户端ID物业(ASP.Net MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net mvc的一个初学者,我有一个疑问:

I'm a begginer in asp.net mvc, and I have a doubt:

我试图做一个文本框的标签在我看来,我想知道,我怎么能采取的ID将在客户端渲染generete脚本...例如:

I'm trying to do a label for a TextBox in my View and I'd like to know, how can I take a Id that will be render in client to generete scripts... for example:

<label for="<%=x.Name.?ClientId?%>"> Name: </label>
<%=Html.TextBoxFor(x=>x.Name) %>

什么需要我把?客户端Id?以确保正确的ID将被渲染到相应的控制?

What need I put in "?ClientId?" to make sure that correct Id will be render to the corresponding control ?

感谢

干杯

推荐答案

将这个code的地方:

Put this code somewhere:

using System; 
using System.Linq.Expressions; 
using System.Web.Mvc; 

namespace MvcLibrary.Extensions 
{ 
    public static class HtmlExtensions 
    { 
        public static MvcHtmlString FieldIdFor<TModel, TValue>(this HtmlHelper<TModel> html,
            Expression<Func<TModel, TValue>> expression) 
        { 
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression); 
            string inputFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); 
            return MvcHtmlString.Create(inputFieldId); 
        } 
    } 
}

然后在您的ASPX视图:

Then in your ASPX view:

<label for="<%= Html.FieldIdFor(m => m.EmailAddress) %>">E-mail address:</label> 
<%= Html.TextBoxFor(m => m.EmailAddress) %>

您也可以使用这个在JavaScript调用,你不会预先知道控件的ID,可能需要它一些JavaScript code反对它的工作:

You can also use this in JavaScript calls as you won't know the control's ID in advance and may need it for some JavaScript code to work against it:

<script> $.CoolJQueryFunction('<%= Html.FieldIdFor(m => m.EmailAddress) %>'); </script>

该LabelFor HTML辅助方法,有人讲到这里,不会让你指定要使用的实际文本标签,你有一个属性来设置标签文字来装饰你的ViewModels,与恕我直言是丑陋的。我宁愿这些东西出现在实际ASPX视图部分本身,而不是在某些领域/视图模型。有些人会用我虽然不同意。

The LabelFor HTML helper method, that someone mentioned here, won't let you specify the actual text label you want to use, you have to decorate your ViewModels with attributes to set the label text, with IMHO is ugly. I'd rather that stuff appear in the actual ASPX view part itself, not on some domain/view model. Some people will disagree with me though.

不知道的张贴链接到自己的博客文章的规则,但我张贴在这个确切主题的博客:<一href=\"http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2\">http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2

Not sure of the rules for posting links to one's blog posts, but I posted a blog on this exact topic: http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2

这篇关于客户端ID物业(ASP.Net MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆