locked
HtmlFieldPrefix in a partial view breaks Html helpers which take expressions as strings RRS feed

  • Question

  • User1431643137 posted

    Using HtmlFieldPrefix:

     @Html.Partial("_UsersPartial", Model.USERS.First(), new ViewDataDictionary { 
      TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "anything" } })

    or just using the Html.EditorFor

    @Html.EditorFor(m => m.USERS)

    which has an implicit HtmlFieldPrefix, make HtmlHelpers, such as TextBox and Editor, not able to get the value from the model (inside the PartialView/EditorTemplate), i.e. input field is empty.

    Html.TextBox("propertyA")

    produces an empty input even if Model.propertyA != null

    TextBoxFor and EditorFor are working fine.

     

    PS. I am using this property to give the necessary prefixes to my input fields when using partials to displat a nested property.

    Saturday, September 21, 2019 6:52 AM

All replies

  • User-17257777 posted

    Hi illusive7man,

    Note you’re invoking the [`@Html.TextBox(name)` without the value parameter]

    In that case, the value will be null.

    One approach is to specify the value for the field.. See [TextBox(HtmlHelper, String, Object)]

    Index View:

    @Html.Partial("_UsersPartial", Model.USERS.First(), new ViewDataDictionary {
      TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "anything" } })
    

    _UsersPartial View:

    @model Test5.Models.User
    @Html.TextBox("propertyA", Model.Name)
    

    Test Result:

    By the way, you don’t have to pass two parameters by `@Html.TextBox(name, value)`. Using `@Html.TextBoxFor(model=>model.Name)` will also make it.

    Best Regards,

    Jiadong Meng

    Monday, September 23, 2019 10:49 AM