1 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超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 ...";
? ? }
}
然后在視圖中調(diào)用它,不要使用 Displayfor() 并像這樣調(diào)用它:
?<td>
? ? @item(item.DescriptionWithDots)
?</td>
正如 @ath 上面所說,這不是一個很好的做法(因?yàn)槟F(xiàn)在將視圖耦合到模型并跳過控制器),您希望避免調(diào)用視圖中的方法。
相反,您可以將其重構(gòu)到您的控制器中:
foreach (var item in models)
? ? ? ? {
? ? ? ? ? ? item.Description = item.DoTheDots(item.Description);
? ? ? ? }
? ? ? ? return View(models);
- 1 回答
- 0 關(guān)注
- 113 瀏覽
添加回答
舉報(bào)