【问题标题】:MVC .net textbox to texbox area (Razor)MVC .net 文本框到文本框区域(Razor)
【发布时间】:2015-01-16 20:06:14
【问题描述】:

我有一个文本框,我想扩展它并添加几行以使其成为文本区域而不是小框。

原代码

 @Html.EditorFor(x => x.emailBody)

<input type="submit" value="Send Mail" class="btn btn-default" />

我在 SOF 上找到了这个,但它不起作用,而且字段看起来一样

@Html.EditorFor(x => x.emailBody, new { @class = "form-control", @rows = 5 })
<input type="submit" value="Send Mail" class="btn btn-default" />

任何人都可以提出一个简单的解决方案

【问题讨论】:

    标签: c# .net asp.net-mvc-5


    【解决方案1】:
    @Html.TextAreaFor(model => model.emailBody, new { @rows = 3 })
    

    您可以使用 textarea 并根据您的选择设置行,这是一种更好的方式,然后增加普通文本框的行......

    【讨论】:

      【解决方案2】:

      向属性添加DataType.MultilineText 属性,@Html.EditorFor() 将呈现&lt;textarea&gt; 而不是&lt;input type="text" ../&gt;

      [DataType(DataType.MultilineText)]
      public string emailBody { get; set; }
      

      还要注意(假设您使用的是 MVC-5.1+),那么要添加 html 属性,应该是

      @Html.EditorFor(x => x.emailBody, new { htmlAttributes = new { @class = "form-control", @rows = 5 } })
      

      如果您只有 MVC-5,则使用 @Html.TextAreaFor() 或创建自定义 EditorTemplate 并将属性传递为 AdditionalViewData (example here)

      【讨论】:

      • 感谢 4 快速解决方案 :-)
      • 还要注意(我假设您使用 MVC-5.1+)然后添加 html 属性,它应该是 @Html.EditorFor(x =&gt; x.emailBody, new { htmlAttributes = new { @class = "form-control", @rows = 5 } }) 否则您必须使用 @Html.TextAreaFor() 或创建自定义 @987654333 @
      【解决方案3】:

      为什么你不能使用这样的东西

      @Html.TextAreaFor(model => model.emailBody, new { @rows = 3 })
      

      您可以使用 TextAreaFor 代替 EditorFor 在文本框中获取多行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-03
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-28
        相关资源
        最近更新 更多