首页 > 解决方案 > HTML.TextAreaFor ASP.NET MVC“模板只能用于字段访问”

问题描述

这行代码导致以下错误。

代码:

@Html.TextAreaFor(model => XDocument.Parse(model.TramaDetalle).ToString(), 
new { @class = "form-control", @readonly = "readonly", @title = "Detalle" , @style = "height: 320px;resize: none;" })

错误:

模板只能与字段访问、属性访问、一维数组索引或单参数自定义索引器表达式一起使用。

这可以重写还是我不能这样做?

标签: xmlasp.net-mvcrazorrazor-pages

解决方案


你可以这样做。

@var parsedTramaDetalle = XDocument.Parse(model.TramaDetalle).ToString();

@Html.TextAreaFor(model => model.TramaDetalle, 
new { @class = "form-control",@value=parsedTramaDetalle, @readonly = "readonly", @title = "Detalle" , @style = "height: 320px;resize: none;" })

另一种方法是在控制器中解析它并直接使用Htmlhelper

@Html.TextAreaFor(model => model.ParsedTramaDetalle, 
new { @class = "form-control",@value=parsedTramaDetalle, @readonly = "readonly", @title = "Detalle" , @style = "height: 320px;resize: none;" })

推荐阅读