1

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.

1 Answer 1

1

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

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

should be sufficient.

Sign up to request clarification or add additional context in comments.

5 Comments

Perfect - thank you. A quick Q if possible here? Why does class have an '@' in front of it but cols does not?
Because class is a reserved word in Razor :)
I'd love to but "Vote up requires 15 reputation". So unfortunately I'm a disreputable person. :(
You should have 5 more now :)
@ThomasAJ I mean "accept" not vote up. Click the tick under the voting buttons

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.