简体   繁体   中英

Using EditorFor in ASP.NET MVC 2.0 to render a span

I have a complex object that if I render in this way:

<%=Html.EditorFor(x => x.ContactRoleDto.Competencies[0].Name) %>

The above generates the following element which has the name and id tags that I want:

<input Size="40" id="ContactRoleDto_Name" maxlength="100" name="ContactRoleDto.Name" type="text" value="" />

I would like to render a tag with the correct id and name attributes that are in the same form as above, ie ContactRoleDto.Competencies[0].Name".

How is the best way to achieve this?

All I really want is the ability to pull out the correct id and name fields that will help me model bind a table that is dynamically generated or rendered.

Is ModelMetaData the best way to go, I do not want to go the UIHint route.

You could use the ExpressionHelper.GetExpressionText method. For example:

Expression<Func<string, YourModel>> expression = x => x.ContactRoleDto.Competencies[0].Name;
string id = ExpressionHelper.GetExpressionText(expression);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM