Heidel
Heidel

Reputation: 3254

Add text in @Html.TextAreaFor MVC 4

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

Answers (5)

AbdusSalam
AbdusSalam

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

karthik
karthik

Reputation: 11

you add the default value into it.. by

@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow",@value = "added text" })

Upvotes: 1

Jon649
Jon649

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

Chris Diver
Chris Diver

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

AliRıza Adıyahşi
AliRıza Adıyahşi

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

Related Questions