Moo*_*Moo 8 templates asp.net-mvc-3
使用EditorFor模板是ASP.Net MVC 3的一个非常好的功能,但是有可能让EditorFor渲染一个无人居住的模板以允许创建记录吗?
或者还有其他方法可以做到这一点吗?
我试图这样做的方式如下:
@Html.EditorFor(model => model)
@Html.EditorFor(x => new List<Business.ViewModel.Affiliate.Contact>())
@Html.EditorFor(new List<Business.ViewModel.Affiliate.Contact>())
@Html.EditorFor(new Business.ViewModel.Affiliate.Contact())
Run Code Online (Sandbox Code Playgroud)
第一个显然是有效的,但随后的(显示我正在尝试做的)都会失败并出现以下错误:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Run Code Online (Sandbox Code Playgroud)
有问题的模型是:
IEnumerable<Business.ViewModel.Affiliate.Contact>
Run Code Online (Sandbox Code Playgroud)
控制器负责准备将传递给视图的视图模型.因此,如果您需要以5个空联系人行初始化视图模型,则可以在控制器中执行此操作:
public ActionResult Index()
{
var model = new MyViewModel
{
// Add 5 empty contacts
Contacts = Enumerable.Range(1, 5).Select(x => new Contact()).ToList()
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
并在您的视图中像往常一样使用EditorFor助手:
@model MyViewModel
...
@Html.EditorFor(x => x.Contacts)
Run Code Online (Sandbox Code Playgroud)
这将为我们添加到Contacts
集合中的5个元素中的每个元素呈现相应的编辑器模板.
归档时间: |
|
查看次数: |
3660 次 |
最近记录: |