RoyFat
RoyFat

Reputation: 44

Editing the attributes genereted from @Html.EditorFor

I wonder if its possible to edit the attributes value that the @Html.EditorFor(model => item.Title) is generating

the @Html.EditorFor(model => item.Title) would generate this:

<input class="text-box single-line" id="item_Title" name="item.Title" type="text" value="Avatar" />

i wonder if its possible to edit the id attribut?

Thanks!

Upvotes: 0

Views: 918

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Create your own Editor template to custom render whatever you want. See: http://www.codecapers.com/post/Display-and-Editor-Templates-in-ASPNET-MVC-2.aspx You then have control over how the editor html is emitted. you MAY though have to do a bit of lambda parsing magic to get the model variable name. I'd have to research that more - just wanted to give a pointer here in case no one else replied.

In this case though, you 'MAY' have to instead write your own extension method that takes the lambda and creates the names based off that lambda. You can see how the lambda is parsed as an example here: http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Creating-tooltips-using-data-annotations-in-ASPNET-MVC.aspx

this all may not be too helful.. but want to provide at least something else : )

Upvotes: 1

Shane Andrade
Shane Andrade

Reputation: 2675

I don't think you can edit the id since the point of EditorFor is that it's "bound" to the model's property that you're applying it to. If you need a different id, you could try making your own text box with Html.TextBox.

Upvotes: 3

Related Questions