首页 文章

无法在TextAreaFor中设置Asp.net MVC5中的cols

提问于
浏览
1

我正在使用此代码,但cols = 20不断获取HTML . 我见过其他解决方案,但他们似乎没有使用像“new {htmlAttributes = new {etc”这样的语法 . 我是MVC的新手,这个语法是由Scaffolding生成的 . 我没有改变这种语法的知识 . 我希望只添加',cols =“34”',如下所示 .

<div class="form-group"> @Html.LabelFor(model => model.SpendDescription, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextAreaFor(model => model.SpendDescription, new { htmlAttributes = new { @class = "form-control", cols = "34" } }) @Html.ValidationMessageFor(model => model.SpendDescription, "", new { @class = "text-danger" }) </div> </div>

生成的HTML是

<textarea id="SpendDescription" rows="2" name="SpendDescription" htmlattributes="{ class = form-control, cols = 34 }" data-val-maxlength-max="200" data-val-maxlength="Must be less than 200 characters." data-val="true" cols="20"></textarea>

这显然是错误的,因为“htmlattributes etc”变成了一个字符串,'form-control'并没有成为一个类 .

妈的!我记得我将EditorFor的scaffolded代码更改为TextAreaFor(几天前),以便有一个文本区域 . 所以我敢打赌传递htmlAttributes的代码是错误的 .

1 回答

  • 1

    你有一个太多级别的 new {} :)

    @Html.TextAreaFor(model => model.SpendDescription, new { @class = "form-control", cols = "34" })
    

    应该足够了 .

相关问题