ASP.NET MVC 3: Override “name” attribute with TextBoxFor

前端 未结 11 1511
臣服心动
臣服心动 2020-11-28 05:41

Is it possible when using Html.TextBoxFor to override the name attribute?

I have tried with no success. I need to use TextBoxFor to get client side vali

11条回答
  •  难免孤独
    2020-11-28 06:16

    Keep it simple, your already providing the ID you should simply be able to use the method "TextBox" instead of "TextBoxFor" and it will work fine client side and server side. In addition, although the accepted answer will work but will produce duplicate Name attributes on your tag if you inspect it using a browser. The below solution does not have that problem.

    MvcHtmlString Html.TextBox(string name, string value, object htmlAttributes)

    @Html.TextBox(Model.Key + "_Data", Model.Key, new { id = Model.Key + "_Data" }
    

提交回复
热议问题