下面为添加到在控制器中实例化的列表中的每个项目正确呈现标签和文本框。
我需要的是标签显示“FavouriteThingID”的值(即 1,2,3,4,5)而不是文字“FavouriteThingID”。
有任何想法吗?
视图模型
public class FavouriteThingViewModel
{
public int FavouriteThingID { get; set; }
public string FavouriteThingName { get; set; }
}
编辑器模板
@model _5fave.Models.FavouriteThingViewModel
<div class="control-group">
@Html.LabelFor(m => m.FavouriteThingID, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.FavouriteThingName)
@Html.ValidationMessageFor(m => m.FavouriteThingName, null, new { @class = "help-inline" })
</div>
</div>
控制器
var favouriteThings = new List<FavouriteThingViewModel>();
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 1, FavouriteThingName = "Raindrops on roses" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 2, FavouriteThingName = "Whiskers on kittens" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 3, FavouriteThingName = "Bright copper kettles" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 4, FavouriteThingName = "Warm woolen mittens" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 5, FavouriteThingName = "Brown paper packages" });
viewModel.FavouriteThings = favouriteThings;