繁体   English   中英

MVC在EditorFor中渲染局部视图

[英]MVC Rendering a partial view in an EditorFor

除了这里提出的解决方案,我试过但没有用 - 我想知道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>
}

看起来你只是错过了EditorFor和你的局部视图之间的EditorFor 虽然EditorFor 确实使用了部分视图,但更恰当的是,它使用了所谓的“编辑器模板”。 这些只是部分视图,其位置和文件名遵循特定约定。

也就是说,您的局部视图应该位于Views\\Shared\\EditorTemplates (创建目录。默认情况下它不存在。)然后,它应该以它应该使用的类型命名。 在这里,这是Cohort ,所以最终的路径和名称应该是:

Views\Shared\EditorTemplates\Cohort.cshtml

然后, EditorFor将看到您有一个Cohort列表,并使用Cohort.cshtml编辑器模板呈现列表中的每个项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM