I’ve already seen this post: Changing the size of Html.TextBox
But I cannot change to Html.TextAreaFor() because it does not have an attribute for me to set a dynamic default value (I am making an edit page that I want to dynamically fill in the fields of the current data which can then be modified). I also can’t seem to get the EditorFor to work even if I change to [DataType(DataType.MultilineText)] in my viewmodel (all it does it seem to change to textboxfor and my default values do not show up).
My edit view takes in a viewmodel containing two models, an empty form (OfferForm) to be filled with the onscreen fields, and the offer model containing the current data. I want to have textfields for the OfferForm, and dynamically fill them with the contents of the offer model’s data.
Here is my html:
<div class="InLine">
<div class="InLine editor-label-container">
<div class="editor-label">
@Html.LabelFor(model => model.OfferForm.Description)
</div>
</div>
<div class="InLine editor-field-container">
<div class="editor-field">
@Html.TextBoxFor(model => model.OfferForm.Description, new { @Value = @Html.DisplayFor(model => model.Offer.Description)})
@Html.ValidationMessageFor(model => model.Offer.Description)
</div>
</div>
</div>
<div class="InLine">
<div class="InLine editor-label-container">
<div class="editor-label">
@Html.LabelFor(model => model.OfferForm.SMSText)
</div>
</div>
<div class="InLine editor-field-container">
<div class="editor-field">
@Html.TextBoxFor(model => model.OfferForm.SMSText, new { @Value = @Html.DisplayFor(model => model.Offer.SmsText)})
@Html.ValidationMessageFor(model => model.Offer.SmsText)
</div>
</div>
</div>
Is there a way to set a “multiline” attribute in the Html.TextBoxFor()?
The simplest way to proceed would be to create an Html extension method like the following
You could then call it:
It only supports basic features and what not. The actual tag generation was pulled from Mvc3 source with slight changes to support passing the value in. This should work for most cases but you might need to add more overrides. This isn’t very…clean but should get you going. 🙂
To get the extension to show up inside your razor views you edit the
Web.configwithin the Views directory and add the namespace (not the class name) where you put this class. It should then show up in intellisense.