Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • MB 109 posts 398 karma points
    Sep 06, 2019 @ 00:11

    Get Form from FieldViewModel ?

    I am creating a customised dropdown field using the FieldTypes in a custom Theme.

    FieldType.DropDownList.cshtml uses a FieldViewModel and I need to find the form that the fields are children of, because I need to look up some values using the Form-Name.

    Anyone got a clue on how to get a reference to the Form from the FieldViewModel of the field?

  • MB 109 posts 398 karma points
    Sep 06, 2019 @ 00:47

    Ha! - answered my own question.

    Fortunately, the requirement is from the Themed FieldType being called from the Themed Form, as:

    Html.Partial(FormThemeResolver.GetFieldView(Model, f), f)

    So, I just needed to extend that out with a ViewDataDictionary and pass what I needed to the Themed FieldType as:

    Html.Partial(FormThemeResolver.GetFieldView(Model, f), f, new ViewDataDictionary { {"myKey", myValue }} )

    Perhaps not the most elegant of solutions, but it works for now.

  • timeverts 2 posts 22 karma points
    May 10, 2021 @ 05:39

    Note that passing a new instance of the ViewDataDictionary object will overwrite any of Umbraco Forms' existing view data needed to render the field. For example, view data that has been set through calling methods like @Html.SetFormFieldWrapperClass("form-group") will be lost and the wrapper CSS class will not be applied to fields.

    So instead, it's probably better to customise the existing ViewData instance and pass that. For example:

        ViewData["myKey"] = myValue; 
    Html.Partial(FormThemeResolver.GetFieldView(Model, f), f, ViewData)
Please Sign in or register to post replies

Write your reply to: