How to get HTML ID created by asp.net MVC EditorFor

I use HTML helpers to display my fields:

<%=Html.EditorFor(m => m.User.Surname)%> 

and the result will be something like this:

 <input class="text-box single-line" id="User_Surname" name="User.Surname" type="text" value="" /> 

Helper uses class Name + '.' fieldName to create an identifier.
I need to pass id to jQuery function. Is there any way to do this without hard coding? Maybe an assistant that can use ASP.NET MVC2 conventions?

+61
asp.net-mvc asp.net-mvc-2
Jan 28 2018-11-21T00:
source share
4 answers

I followed the Mac answer instructions here , and I created my own custom extension:

 public static class HtmlHelperExtensions { public static string HtmlIdNameFor<TModel, TValue>( this HtmlHelper<TModel> htmlHelper, System.Linq.Expressions.Expression<Func<TModel, TValue>> expression) { return (GetHtmlIdNameFor(expression)); } private static string GetHtmlIdNameFor<TModel, TValue>(Expression<Func<TModel, TValue>> expression) { if (expression.Body.NodeType == ExpressionType.Call) { var methodCallExpression = (MethodCallExpression)expression.Body; string name = GetHtmlIdNameFor(methodCallExpression); return name.Substring(expression.Parameters[0].Name.Length + 1).Replace('.', '_'); } return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1).Replace('.', '_'); } private static string GetHtmlIdNameFor(MethodCallExpression expression) { var methodCallExpression = expression.Object as MethodCallExpression; if (methodCallExpression != null) { return GetHtmlIdNameFor(methodCallExpression); } return expression.Object.ToString(); } } 

I imported the application namespace

 <%@ Import Namespace="MvcApplication2" %> 

and finally, I can use my code as follows:

 <%=Html.HtmlIdNameFor(m=>m.Customer.Name)%> 
+7
Jan 29 '11 at 3:19
source share

In ASP.NET MVC 4, Html.IdFor () is built in , which can return this:

 @Html.IdFor(m => m.User.Surname) 
+131
May 28 '13 at 17:57
source share

Look at this question: get the generated clientid for the form field , this is my answer:

I use this helper:

 public static partial class HtmlExtensions { public static MvcHtmlString ClientIdFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { return MvcHtmlString.Create( htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId( ExpressionHelper.GetExpressionText(expression))); } } 

Use it just like any other helper: @Html.ClientIdFor(model=>model.client.email)

+44
Aug 18 2018-11-11T00:
source share

if you have not created your own User.Surname EditorTemplate, which you pass in some HTML attributes, you cannot do this with EditorFor

However you can use TextBoxFor and set id

 <%= Html.TextBoxFor(m => m.User.Surname, new { id = "myNewId" })%> 



Regarding getting this element using the jQuery selector, you can achieve this without the need for hard code using some of the end-with selector . Using this, you can probably still use EditorFor and just select it with $("[id^='Surname ]") `. If you are trying to select this particular item, there really is no way to NOT SURE ANYTHING ANYTHING in jQuery code.

+7
Jan 28 2018-11-11T00:
source share



All Articles