textarea的默认值不起作用

时间:2017-06-24 22:12:49

标签: razor asp.net-core asp.net-core-tag-helpers

我的剃须刀视图中有以下内容:

@model Product

 <form ...>
     ...
     <div class="form-group">
       <label asp-for="Description"></label>
          <textarea asp-for="Description" class="form-control" cols="20" rows="3">Why is this default value disappearing?</textarea>
     </div>
     ...
  </form>

对于某些未知的魔法,默认的textarea值会消失。

2 个答案:

答案 0 :(得分:0)

Textarea使用TextAreaTagHelper进行asp-for属性处理,并使用tagHelper生成textarea而不使用您设置的值,为此您可以在ViewModel中使用以下语法:

public class Product
{
    ...
    public string Description{get; set;} => "Why is this default value disappearing?";
    ...
}

答案 1 :(得分:0)

可以使用模型的构造函数默认绑定元素:

public class Product()
{
    public string Description { get; set; }
    //other properties

    public Product() //the constructor
    {
      Description = "Put the default value here!";
      //other default values
     }
}
相关问题