Model view controller 不属于模型的列表的MVC EditorTemplate
在我的视图中,我调用partialview来获取列表。在该局部视图中,我将该列表分为两个IEnumerable,对于每个列表,我希望调用ModelType的EditorTemplate: 我的观点:Model view controller 不属于模型的列表的MVC EditorTemplate,model-view-controller,mvc-editor-templates,editortemplates,Model View Controller,Mvc Editor Templates,Editortemplates,在我的视图中,我调用partialview来获取列表。在该局部视图中,我将该列表分为两个IEnumerable,对于每个列表,我希望调用ModelType的EditorTemplate: 我的观点: @model List<ModelType> @using System.Collections; @{ int countModelTypeLeft = (Model.Count % 2 != 0) ? Model.Count + 1 : Model.Co
@model List<ModelType>
@using System.Collections;
@{
int countModelTypeLeft = (Model.Count % 2 != 0) ? Model.Count + 1 : Model.Count ;
int countModelTypeRight = Model.Count;
IEnumerable<ModelType> modelTypeListLeft = Model.Take(countModelTypeLeft);
IEnumerable<ModelType> modelTypeListRight = Model.Range(countModelTypeLeft , countModelTypeRight );
}
<div class="modeltype-left" style="float: left; width: 50%;">
// How can I call EditorFor for modelTypeListLeft now?
</div>
<div class="modeltype-right" style="float: right; width: 50%;">
// How can I call EditorFor for modelTypeListRight now?
</div>
@型号列表
@使用系统集合;
@{
int countModelTypeLeft=(Model.Count%2!=0)?Model.Count+1:Model.Count;
int countModelTypeRight=Model.Count;
IEnumerable modelTypeListLeft=Model.Take(countModelTypeLeft);
IEnumerable modelTypeListRight=Model.Range(countModelTypeLeft,countModelTypeRight);
}
//现在如何调用modelTypeListLeft的编辑器?
//我现在如何调用ModelTypeList的编辑器?
如您所见,我被卡住了,因为我无法调用EditorFor,因为modelTypeListLeft和countModelTypeRight这两个列表不是局部视图中给定模型的一部分。如何解决此问题?如果您有ModelType的编辑器模板,那么它仍然可以工作并使用正确的编辑器模板
<div class="modeltype-left" style="float: left; width: 50%;">
@foreach(var leftItem in modelTypeListLeft )
{
Html.EditorFor(m=>leftItem)
}
</div>
<div class="modeltype-right" style="float: right; width: 50%;">
@foreach(var rightItem in modelTypeListRight)
{
Html.EditorFor(m=>rightItem)
}
</div>
@foreach(modelTypeListLeft中的变量leftItem)
{
EditorFor(m=>leftItem)
}
@foreach(modelTypeListRight中的var rightItem)
{
EditorFor(m=>rightItem)
}
我想利用调用@Html.EditorFor
的事实,使用IEnumberable可以让MVC为给定模型选择正确的编辑器模板。但是你的回答给了我一个正确的提示:@Html.EditorFor(model=>modelTypeListLeft)
为列表中的每个项目调用EditorTemplate!但无论如何,我不知道MVC中的参数表达式总是意味着我必须给出给定模型的一部分,但你只是证明了情况并非如此。如何了解更多有关表达式的信息?:),不确定我可以向您提供哪些资源。。。编写更多代码肯定会有所帮助;)如果这个答案有意义,你能把它标为答案吗