asp.net-mvc 在@Html.TextAreaFor MVC 4 中添加文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16645794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:51:45  来源:igfitidea点击:

Add text in @Html.TextAreaFor MVC 4

asp.net-mvcasp.net-mvc-4

提问by Heidel

I need to create the textarea field at my form

我需要在我的表单中创建 textarea 字段

 <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

 <textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2"></textarea>

How can I add text in?

怎么在里面添加文字?

回答by Chris Diver

The content of the text box will be whatever the value of model.Messageis.

文本框的内容将是任何值model.Message

This should be set in your Actionmethod in the Controller, and passed to the View

这应该在您的Action方法中设置Controller,并传递给View

public ViewResult ActionName() {
    var model = new ViewModel(); 
    model.Message = "Text Area Content"; 
    return View(model); 
}

As a test just output model.Messagein the View, it will be empty.

作为测试只是model.Message在 中输出View,它将为空。

 <p>@Model.Message</p> 

Then @Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })will output

然后@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })会输出

 <textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2">Text Area Content</textarea>  

回答by Jon649

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" ;

如果你没有任何像换行符 \n 这样的控制字符,你可以用 var x = "Text" 替换 var x=@Html......

回答by AliR?za Ad?yah?i

@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.

但是如果你在控制器 Get 方法中初始化你的模型,那么它会自动向你的文本区域添加文本。

Check thesetutorials

检查这些教程

回答by karthik

you add the default value into it.. by

您将默认值添加到其中..

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

回答by asalam345

In Controller Write

在控制器写入

ModelVariable.Message= Server.HtmlDecode(ModelVariable.Message);

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

It has worked for me

它对我有用