spa*_*223 3 c# asp.net-mvc model view razor
除了此处建议的解决方案之外,我尝试过但没有奏效 - 我想知道 razor 如何呈现强类型的局部视图?我做了建议的事情,但感觉它没有正确捆绑并且缺少一些东西。
我的“子”模型:
public class Cohort
{
public bool ukft { get; set; }
public bool ukpt { get; set; }
...etc
}
Run Code Online (Sandbox Code Playgroud)
我的强类型部分视图:
@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 />
Run Code Online (Sandbox Code Playgroud)
我的主要模型(包含 Cohort 对象列表):
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后是我的 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>
}
Run Code Online (Sandbox Code Playgroud)
看来您只是错过了EditorFor和您的部分视图之间的联系。虽然EditorFor 确实使用了部分视图,但更合适的是,它使用了所谓的“编辑器模板”。这些只是部分视图,其位置和文件名遵循特定约定。
也就是说,您的部分视图应该进入Views\Shared\EditorTemplates. (创建目录。默认情况下它不存在。)然后,它应该以它应该使用的类型命名。在这里,就是这样Cohort,所以最终的路径和名称应该是:
Views\Shared\EditorTemplates\Cohort.cshtml
Run Code Online (Sandbox Code Playgroud)
然后,EditorFor将看到您有一个Cohorts列表,并使用Cohort.cshtml编辑器模板呈现列表中的每个项目。
| 归档时间: |
|
| 查看次数: |
3910 次 |
| 最近记录: |