Alternate for custom forms input

Topics: Core, General
Feb 25, 2015 at 7:15 PM
Hello all,

I'm trying to override the name given to a custom forms input that I'm displaying in the front end. Via an alternate, I can override it fine, but am having a hard time getting the actual text input to render in the front end.

So I have a file called:

EditorTemplate-Recommendation Parts-FirstFriendName.cshtml

Which is overriding an input field called FirstFriendName, part of Recommendation Parts.

How do I render the actual input?

I've tried @Model.Model.Value and others without success.
Feb 25, 2015 at 8:47 PM
Try Model.Model.Name or Model.Model.DisplayName

Feb 26, 2015 at 3:15 AM
Thanks jtkech, but I had tried those before. It correctly outputs the name of the input, as in "FirstFriendName", but it doesn't output the html <input> element.
Feb 26, 2015 at 6:08 AM
I see, maybe you need to output all the <fieldset> with <label> and <input> elements. And we don't have an original *.cshtml template view to see how it's done from the model, this because it's coming from source code. I think it's not so hard, I will see it tomorrow

Feb 26, 2015 at 9:10 PM
I've found some solutions by using in your alternate the dynamic Model: Model.ContentField.DisplayName, Model.ContentItem.ContentType... By adding a reference to "Orchard.Field" in the "Themes" project, I was able to use field.PartFieldDefinition.Settings.GetModel<InputFieldSettings>()... But no so simple to take into account all properties and input field settings...

One solution: In fact, there is an original template view that do all of that, see the Orchard.Fields/Views/EditorTemplates/Fields/Input.Edit.cshtml. But here the "Model" is equal to your "Model.ContentField" (here, also equal to "Model.Model"), so you can't copy paste this code in your alternate... At this point, there is one solution but where you have to add the Orchard.Field project reference to the Themes project (otherwise the jit compilation fails). First create a 2nd alternate view with the name you want, e.g "yourTheme/Views/Recommendation-FirstFriendName.cshtml". Copy / paste the code of the "Input.Edit.cshtml" file and do what you want (e.g override the display name). Then, in your first alternate "EditorTemplate-Recommendation Parts-FirstFriendName.cshtml", do this
@using Orchard.Fields.Fields;
    Html.RenderPartial("Recommendation-FirstFriendName", (InputField)Model.ContentField);
Better solution: Don't use your 1st alternate and copy the Orchard.Fields/Views/EditorTemplates/Fields/Input.Edit.cshtml file in yourTheme/Views//EditorTemplates/Fields/Input.Edit.cshtml. It works, but at this point we can't use a more specialized name for this view. So, we have to check in the code if it's in the context of your custom form type and specific field. So, in the c# code part of this alternate, add this
var displayName = Model.DisplayName;
if (Html.FieldNameFor(m => m).Equals("YourCustomFormType.YourFieldName", StringComparison.OrdinalIgnoreCase)) {
    displayName = "What you want";
Then, use the displayName variable in the html part of the view, this to override the DisplayName. You can do the same for other properties

Note: Without any alternates, take a look, with the "Shape Tracing" module or in the page source file, on the final html rendering, this to check the correct content type and field name to use above. This because in your 1st alternate name there is a space for your content type

Mar 1, 2015 at 12:00 AM
Thanks a lot for the very detailed reply jtkech. I look forward to trying out the Better Solution this next week at work.