为什么我的复选框没有生成?

时间:2016-02-14 11:41:33

标签: asp.net-mvc asp.net-mvc-4

我有以下控制器使用生成一组checkboxes

的模板调用视图

控制器/动作

    public class CompoundsController : Controller
    {
        //
        // GET: /Compounds/
        testdbDBContext db = new testdbDBContext();

        public ActionResult Index()
        {
            var comps = db.Compounds.Select(e => new CompoundModel { Id=e.Id, Code=e.Code, Name=e.Name, IsSelected = e.IsSelected }).ToList();

            return View(new ChemicalsVM { cModel = comps});
        }
}

视图

@model mvca1.Models.ChemicalsVM

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ChemicalsVM</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.SName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.SName)
            @Html.ValidationMessageFor(model => model.SName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DName)
            @Html.ValidationMessageFor(model => model.DName)
        </div>

        <div class="editor-label">
            @Html.Label("Chemicals")
        </div>
        <div class="editor-field">
            @Html.EditorFor(x=>x.cModel)
            @Html.ValidationMessageFor(model => model.cModel)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

模板是:

@model mvca1.Models.CompoundModel

@{
    ViewBag.Title = "rmplCompoundDB";
}

@Html.HiddenFor(x=>x.Id)
@Html.HiddenFor(x=>x.Code)
@Html.CheckBoxFor(x=>x.IsSelected, (Model.IsSelected)? new {@checked="checked"}:null)
@Html.LabelFor(x=>x.Name)

当我运行代码而不是使用标签生成checkboxes时,它会显示ID号(应该隐藏)

以下是https://youtu.be/1Z2fwfBgyn8

的视频

为什么它没有按预期显示checkboxes

更新:

这是我的ChemicalsVM

   public class ChemicalsVM
    {
        public int Id { get; set; }
        public string SName { get; set; }
        public string DName { get; set; }

        public List<CompoundModel> cModel { get; set; }

        public ChemicalsVM()
        {
            cModel = new List<CompoundModel>();
        }

    }

这是CompoundModel

 public class CompoundModel
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

正在呈现的html意味着ViewEngine未找到EditorTemplate。模板必须与类名称相同(即CompoundModel.cshtml),并且位于/Views/Shared/EditorTemplates/Views/Compounds/EditorTemplates文件夹中。