Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

@Html.TextBoxFor OR @Html.TextBox on viewcomponent (block) generating name with both contentarea name & currentpage



I want to display form via block (viewcomponent), but the form is generating textbox with contentarea & currentpage name.

e.g ->

 @Html.TextBoxFor(x => x.Test.Example, new { @class = "form-control" })

Generates ->

<input class="form-control" data-val="true" data-val-length="The field Examlpe must be a string with a maximum length of 15." data-val-length-max="15" id="CurrentPage_TestContentArea_Test_Example" maxlength="15" name="CurrentPage.TestContentArea.Test.Example" type="text" value="123456789">

TestContentArea is contentarea where block is added

How to generate name which only contains viewmodel field name like Test.Example ?

Nov 30, 2023 9:21

Since this looks like a nested class, try doing nameof    @Html.TextBox(nameof(Model.Test.Example), new { @class = "form-control"})

Dec 04, 2023 11:51

I tried but getting the same result -> 

                                  @Html.TextBox(nameof(Model.Test.Example), Model.Test.Example, new {@class="form-control"})  

The second paramter is to set initial value :) 

Dec 04, 2023 12:05

I found a solution. By adding below line on top of razor view file, the html field prefix is removed 

    ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;
Dec 06, 2023 10:46
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.