除了我所尝试但不起作用的here建议的解决方案之外 - 我想知道razor如何计算出一个强类型的局部视图?我按照建议进行了操作,但感觉它没有正确绑定而且缺少某些东西。
我的“子”模型:
public class Cohort
{
public bool ukft { get; set; }
public bool ukpt { get; set; }
...etc
}
我的强类型部分视图:
@model Models.Cohort
@Html.RadioButtonFor(model => Model.ukft, true) <span style="margin-right:8px;">Yes</span>
@Html.RadioButtonFor(model => Model.ukft, false) <span>No</span> <br />
我的主模型(包含群组对象列表):
public class OptOut
{
public int optOutID { get; set; }
public bool hasOptedOut { get; set; }
public List<Cohort> list { get; set; }
public OptOut()
{
List<Cohort> list = new List<Cohort>();
list.Add(new Cohort());
list.Add(new Cohort());
list.Add(new Cohort());
list.Add(new Cohort());
this.list = list;
}
}
然后是我的HTML:
@model Models.OptOut
@using (Html.BeginForm("OptedOut", "Home"))
{
//this should supposedly figure out to render a partial view for each element in the list
@Html.EditorFor(model => model.list)
<div class="form-group" style="margin-top:25px;">
<input id="confirm" type="submit" value="Confirm" class="btn btn-success btn-lg"/>
</div>
}
答案 0 :(得分:3)
您似乎错过了EditorFor
与部分视图之间的联系。虽然EditorFor
使用部分视图,但更恰当的是,它使用了所谓的&#34;编辑模板&#34;。这些只是部分视图,其位置和文件名遵循特定约定。
即,您的部分视图应该放在Views\Shared\EditorTemplates
中。 (创建目录。默认情况下它不存在。)然后,它应该以它应该使用的类型命名。在这里,Cohort
,所以最终的路径和名称应为:
Views\Shared\EditorTemplates\Cohort.cshtml
然后,EditorFor
会看到您有一个Cohort
列表,并使用Cohort.cshtml
编辑器模板呈现列表中的每个项目。