November Happy Hour will be moved to Thursday December 5th.

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

ZZ
ZZ
Vote:
 

Hi,

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 ?

#313407
Nov 30, 2023 9:21
Vote:
 

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

#313650
Dec 04, 2023 11:51
ZZ
Vote:
 

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 :) 

#313651
Dec 04, 2023 12:05
ZZ
Vote:
 

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;
}
#313770
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.