如何向剃刀助手添加参数

ehs*_*san 5 html c# asp.net-mvc html-helper razor

我想知道HTML助手怎么样写@Html.TextBoxFor(model => model.signature)data-id辅助性生产中的输入参数如下图所示.

<input type="text" name="signature"  data-id="No Signature" />
Run Code Online (Sandbox Code Playgroud)

注1:参数之类dataId的工作是htmlAttributes因为它是一个简单的变量.

注2:我知道扩展方法和使用属性 @{var attributes = new Dictionary<string, object>{{ "data-id", "No Signature" }};}

我觉得必须有更好的方法来解决这个问题.任何的想法...?

非常感谢.

Dar*_*ren 1

您可以创建自己的自定义助手,例如:

 public static class TextBoxExtensions
     {
          public static string CustomTextBox(this HtmlHelper helper, string name)
          {
               return String.Format("<input type='text' name={0} data-id='No Signature'></input>", name);
          }
     }
Run Code Online (Sandbox Code Playgroud)

然后在您的视图中您可以执行以下操作:

@Html.CustomTextBox("signature");
Run Code Online (Sandbox Code Playgroud)