代码之家  ›  专栏  ›  技术社区  ›  Roman Pokrovskij Archil Labadze

输入的标记帮助程序[asp for]不能处理字节的数组值(与@Html.HiddenFor相反)

  •  0
  • Roman Pokrovskij Archil Labadze  · 技术社区  · 7 年前

    @Html.HiddenFor(e=>e.RowVersion) 工作良好并产生:

    <input id="RowVersion" name="RowVersion" type="hidden" value="AAAAAAAARlI=" /> 
    

    但是标签助手版本 <input asp-for="@Model.RowVersion" name="RowVersion" hidden /> 生成:

    <input name="RowVersion" hidden id="RowVersion" value="System.Byte[]" />
    

    为了保持一致性,我想继续使用标记助手版本。如何启用字节数组序列化?

    1 回复  |  直到 7 年前
        1
  •  1
  •   David Liang    7 年前

    使用 type="hidden" 而不是 hidden 属性!

    如果你做到了,你应该也能做到同样的事情

    /*
     *   From the ViewModel:
     *       byte[] RowVersion = Encoding.UTF8.GetBytes("FR")
     */
    <input asp-for="RowVersion" type="hidden" />
    

    比较

    enter image description here enter image description here

    原因(我不是百分之百确定)

    标记助手 asp-for type HTML输入的属性。如果找不到适合HTML输入的类型,则默认为 type="text"

    这就是为什么你 <input asp-for="RowVersion" hidden /> 将生成隐藏的文本框。生成文本框时,标记帮助器不清除输入值:

    enter image description here

    但是如果你指定 type=“隐藏” 你的财产类型是 byte[]

    enter image description here

    这就是为什么 @Html.HiddenFor() 以及 <input type="hidden" asp-for= />