ThomasAJ
ThomasAJ

Reputation: 721

Cannot set cols in Asp.net MVC5 in TextAreaFor

I am using this code but cols=20 keeps getting HTMLed. I have seen other solutions but they do not seem to be using syntax like "new { htmlAttributes = new { etc". I am new to MVC and this syntax was generated by Scaffolding. I do not have the knowledge to change this syntax. I was hoping to just add ', cols = "34"' as per below.

<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>

The generated HTML is

<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>

Which is obviously wrong as "htmlattributes etc" became a string, 'form-control' did not become a class.

DARN! I just remembered that I changed the scaffolded code of EditorFor to TextAreaFor (a few days back) in order to have a text area. So I bet the code that passes htmlAttributes is wrong.

Upvotes: 1

Views: 424

Answers (1)

Jane S
Jane S

Reputation: 1487

You have one too many levels of new {} :)

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

should be sufficient.

Upvotes: 1

Related Questions