1 回答

TA贡献1846条经验 获得超7个赞
您可以将该方法定义(编写)为新闻模型类的成员方法
public class NewsModel
{
//all your properties here
public string Description { get; set; }
public string DescriptionWithDots { get { return DoTheDots(Description); } }
//the method that writes the dots
public string DoTheDots(string input)
{
return input + "some dots ...";
}
}
然后在视图中调用它,不要使用 Displayfor() 并像这样调用它:
<td>
@item(item.DescriptionWithDots)
</td>
正如 @ath 上面所说,这不是一个很好的做法(因为您现在将视图耦合到模型并跳过控制器),您希望避免调用视图中的方法。
相反,您可以将其重构到您的控制器中:
foreach (var item in models)
{
item.Description = item.DoTheDots(item.Description);
}
return View(models);
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报