本文介绍了如何EditorFor设置ID和表单元素名称的HTML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套自定义用户控件的几层:

I have few layers of nesting custom user controls:

RegisterUser.aspx

RegisterUser.aspx

 <%@ .... Inherit="System.Web.Mvc.ViewPage<RegisterUserViewModel>"
 ...
 <%= Html.EditorFor(m => m.Details) %>
 ...

UserDetails.ascx

UserDetails.ascx

 <%@ .... Inherit="System.Web.Mvc.ViewUserControl<UserDetails>"
 ...
 <%= Html.EditorFor(m => m.BirthDate) %> <!--BirthDate is of type DateTime-->
 ...

和我宣布DateTime.ascx共享/ EditorTemplates

and I have declared DateTime.ascx in Shared/EditorTemplates

 <%@ .... Inherit="System.Web.Mvc.ViewUserControl<dynamic>"
 ...
 <input type="text" id="???" />
 ...

现在的问题是如何设置的输入ID属性?我知道EditorFor使一些神奇的默认类型。例如,如果日期时间是字符串类型,EditorFor将设置输入类型为Details_BirthDate而得名属性为Details.BirthDate的ID。我想知道它是如何做?因为我想用它为我的特别定制的类型。

The question is how to set input id attribute? I know that EditorFor makes some magic for default types. For example if DateTime was of type string, EditorFor will set id of input type to "Details_BirthDate" and the name attribute to "Details.BirthDate". I want to know how it's done? Because I want to use it for my special custom types.

推荐答案

使用以下两种方法找到了解决办法:

Found the solution using the following two methods:

<%= Html.ViewData.TemplateInfo.GetFullHtmlFieldId("BirthDate") %>

<%= Html.ViewData.TemplateInfo.GetFullHtmlFieldName("BirthDate") %>

这篇关于如何EditorFor设置ID和表单元素名称的HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:51