I’m trying to populate new HTML5 input attribute values into a Razor partial view. My model looks like this:
public class Answer
{
public int AnswerId { get; set; }
public string AnswerText { get; set; }
public int? Columns { get; set; }
public int? Maximum { get; set; }
public string Placeholder { get; set; }
}
My partial view looks like this:
@model Answer
@{Layout = null;}
@Html.TextBoxFor(x => x.AnswerText, new { @class="textbox", cols="@Model.Columns", max="@Model.Maximum", placeholder="@Model.Placeholder" })
Probably unsurprisingly this generates html that looks like this (I’ve left out some irrelevant attributes):
<input type="text" class="textbox" cols="@Model.Columns", max="@Model.Maximum", placeholder="@Model.Placeholder">
Whereas what I’m after is html that looks like this, but with whatever value happpens to be in the model:
<input type="text" class="textbox" cols="50", max="30", placeholder="Answer here">
I’m sure this is me having a Homer Simpson moment but I just can’t get it to work.
Take out the quotes and use GetValueOrDefault on the nullable fields: