Reputation: 3254
I need to create the textarea field at my form
<textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2">Message</textarea>
I write code
@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })
but I get empty textarea without any text
<textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2"></textarea>
How can I add text in?
Upvotes: 1
Views: 40828
Reputation: 440
In Controller Write
ModelVariable.Message= Server.HtmlDecode(ModelVariable.Message);
@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })
It has worked for me
Upvotes: 0
Reputation: 11
you add the default value into it.. by
@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow",@value = "added text" })
Upvotes: 1
Reputation: 289
After much scratching around I managed to get it done this way.
@{ var _address="Line 1\nLine 2\n Line 3"; }
@Html.TextArea(Model.xxxx , new { id="address" } )
<script>
var x = @Html.Raw(Json.Encode( _address )) ;
document.getElementById("address").value = x;
</script>
If you haven't any control characters like newline \n you can replace var x=@Html...... with var x = "Text" ;
Upvotes: 3
Reputation: 19802
The content of the text box will be whatever the value of model.Message
is.
This should be set in your Action
method in the Controller
, and passed to the View
public ViewResult ActionName() {
var model = new ViewModel();
model.Message = "Text Area Content";
return View(model);
}
As a test just output model.Message
in the View
, it will be empty.
<p>@Model.Message</p>
Then @Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })
will output
<textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2">Text Area Content</textarea>
Upvotes: 5
Reputation: 15866
@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow", value = "added text" })
But If you initialize your model in controller Get method, then it will add text to your textarea, automaticly.
Check these tutorials
Upvotes: 2