ASP.NET MVC - How can I set the Default Value in a Strongly Typed TextArea?

冷暖自知 提交于 2019-12-01 09:09:31

问题


I'm trying to make a TextArea have a default value.

<%: Html.TextAreaFor(Function(model) model.Description, 5, 10, New With {.Value = "Description: "})%>

This works properly in a TextBoxFor but doesn't work in a TextAreaFor

Am I missing something very obvious?


回答1:


You can specify the value for Description property in the controller action when creating the model, and pass that model to the View:

public ViewResult Create()
{
    var model = new MyPageModel() 
    {
        Description = "Description: ";
    }

    return View(model);
} 

In the view:

<%: Html.TextAreaFor(model.Description) %>

Setting the value won't work as the contents of a TextArea are specified between the opening and closing tags, not as an attribute.



来源:https://stackoverflow.com/questions/3655033/asp-net-mvc-how-can-i-set-the-default-value-in-a-strongly-typed-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!