ASP.Net MVC TextArea无法更改宽度

时间:2016-10-18 13:46:21

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

我第一次使用ASP.Net MVC,我正在尝试设置textarea的宽度。

这应该(并且很可能是)非常简单,但无论我尝试什么,该区域的宽度都不会改变。

到目前为止,我已尝试过以下内容:

@Html.TextAreaFor(m => m.RequestDetails, 10, 10, htmlAttributes: new {@class = "form-control", })
@Html.EditorFor(m => m.RequestDetails, new { htmlAttributes = new { @class = "form-control" } })
@Html.TextAreaFor(m => m.RequestDetails , new { @class = "form-control", @cols = 80, @rows = 10 })

我能够改变行数没问题。

最初,我认为这是由引导程序和网格系统引起的,所以我把它从网格中拿出来,然后再次使用上述所有内容并得到同样的问题。

我看过这里:

  1. asp.net-mvc How to change width Html.TextBox

  2. TextAreaFor Cannot Set Width

  3. asp.net-mvc How to change width Html.TextBox

  4. 以及其他一些Stack问题以及我认为可行的所有内容都没有。

    此特定属性的ViewModel是

    [Display(Name = "What is required")]
    [DataType(DataType.MultilineText)]
    [StringLength(4000, MinimumLength=50, ErrorMessage="Please provide more information about your requirements")]
    public string RequestDetails { get; set; }
    

    我希望这会跨越它将要进入的div的宽度,所以非常感谢一些帮助。

    对我来说,最后一个方法是使用HTML控件而不是Razor,以这种方式获取数据,但我相信这应该很容易实现我想要的。它只是我做错了。 感谢

    西蒙

2 个答案:

答案 0 :(得分:2)

这里的问题是处理max-width

这是正确的实施:

@Html.TextAreaFor(m => m.RequestDetails, 10, 10, htmlAttributes: new {@class = "form-control", })

你所要做的就是给那个文本区域另一个类名,然后在CSS中设置该类的最大宽度,如下所示:

<强>剃刀/ HTML

@Html.TextAreaFor(m => m.RequestDetails, 10, 10, htmlAttributes: new {@class = "form-control width-textarea", })

<强> CSS

.width-textarea{
    max-width: /* whatever you please */ !important
}

请告诉我这是否有帮助!

答案 1 :(得分:0)

<style>
    .areaWidth
    {
        width: 100px;
    }
</style>

@Html.TextAreaFor(m => m.RequestDetails, new { @class = "form-control areaWidth" })