Help me understand "EditorTemplates"
# help-with-umbraco
c
Hi there - I'm using the default RegisterMember partial which renders a Textbox for all properties added to a member. I'd like to change one of these to a checkbox but haven't got the slightest clue how. There's a comment in the partial that tells me how I "can easily change it." Apparently by adding a custom view in
~/Views/Shared/EditorTemplates/
. I can sort of follow that - but I have no idea how they get from
@Html.EditorFor(m => registerModel.MemberProperties[i].Value)
to this output:
<input class="text-box single-line" id="registerModel_MemberProperties_3__Value" name="registerModel.MemberProperties[3].Value" type="text" value="">
(e.g. the '3' here is not sent in, so how does the view get this information?) - how am I supposed to "easily" do this? Thanks!
r
I believe the id and name are generated using
@Html.IdFor(m => m.Property)
and
@Html.NameFor(m => m.Property)
. The
3
comes from the value of
i
when you pass it into the
EditorFor
method, and these two methods pick that up and give you the appropriate value.
c
Thanks Rachel – it sounds right, but I'm only passing a
string
into the method (AFAIK). How does it get all that info from that?
r
If you're using a lambda expression (the arrow syntax, e.g.
m => registerModel.MemberProperties[i].Value
), it can use reflection to get the metadata rather than the value of that expression (if I'm remembering right)
So it'll know the name of the thing, its type, etc etc
r
If you use the tag helper
asp-for
it should accept the expression as a string, not sure if it works with
EditorFor
though as it's meant to be an alternative https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-8.0#expression-names
c
Thanks - that does actually help demystify some of the seemingly "magic" stuff going on... let's see if that helps me create a custom view for the checkbox then...
2 Views