我有一个部分视图,我将模型传递给它,它显示用户可以选择的复选框,最终会使用我的表单发布,以便我可以查询其检查状态并根据需要将数据插入数据库。
我想更改我的局部视图,以便不是采用包含模型列表的模型,而是直接获取模型列表,从而将其解耦并使其作为局部视图更有用。问题是,当我这样做时,似乎没有正确绑定数据。下面是之前的代码示例(正常工作和不工作之后)。
如果有人能够解释为什么我的更改没有生效,我会非常感激,例如在检查发布的数据时,在检查发布的数据后,列表为空并在之前正确填充之前:
之前:
调用局部视图:
<fieldset>
<legend>Facilities</legend>
<div class="display-field">
<div>
@{Html.RenderPartial("Partial/FacilityPartial", Model);}
</div>
</div>
</fieldset>
部分观点:
@model namespace.Models.SchoolRegisterModel
@for (var i = 0; i < Model.Facilities.Count; i++)
{
@Html.HiddenFor(x => x.Facilities[i].name)
@Html.HiddenFor(x => x.Facilities[i].facility_id)
@Html.LabelFor(x => x.Facilities[i].@checked, Model.Facilities[i].name);
@Html.CheckBoxFor(
x => x.Facilities[i].@checked,
new
{
id = Model.Facilities[i].facility_id,
@class = "RightSpacing",
description = Model.Facilities[i].description
}
)
}
后:
调用局部视图:
<fieldset>
<legend>Facilities</legend>
<div class="display-field">
<div>
@{Html.RenderPartial("Partial/FacilityPartial", Model.Facilities);}
</div>
</div>
</fieldset>
部分观点:
@model IEnumerable<namespace.Models.facility>
@for (int i = 0; i < Model.Count(); i++)
{
@Html.HiddenFor(x => x.ElementAt(i).name)
@Html.HiddenFor(x => x.ElementAt(i).name)
@Html.HiddenFor(x => x.ElementAt(i).facility_id)
@Html.LabelFor(x => x.ElementAt(i).@checked, Model.ElementAt(i).name);
@Html.CheckBoxFor(
x => x.ElementAt(i).@checked,
new
{
id = Model.ElementAt(i).facility_id,
@class = "RightSpacing",
description = Model.ElementAt(i).description
}
)
}
提前致谢!
乔
完成代码(工作)
型号:
public class SchoolRegisterModel
{
// ... Lots of data
public string Data { get; set; }
[Display(Name = "Facilities")]
public IEnumerable<FacilityViewModel> Facilities { get; set; }
}
namespace namespace.Models
{
public class FacilityViewModel
{
public FacilityViewModel()
{
}
public FacilityViewModel(facility facil, bool bSelected = false)
{
this.Facility = facil;
this.Selected = bSelected;
}
public facility Facility { get; set; }
public bool Selected { get; set; }
}
}
编辑模板:
@model namespace.Models.FacilityViewModel
@Html.HiddenFor(x => x.Facility.facility_id)
@Html.HiddenFor(x => x.Facility.name)
@Html.LabelFor(x => x.Selected, Model.Facility.name)
@Html.CheckBoxFor(x => x.Selected,
new
{
id = Model.Facility.facility_id,
@class = "RightSpacing",
description = Model.Facility.description
}
)
控制器:
// GET: /School/Register
public ActionResult Register()
{
var viewModel = new SchoolRegisterModel();
PopulateFacilityCollection(ref viewModel);
return View(viewModel);
}
private void PopulateFacilityCollection(ref SchoolRegisterModel viewModel)
{
List<FacilityViewModel> FacilityCollection = new List<FacilityViewModel>();
foreach (facility facil in DBModel.facilities)
{
FacilityCollection.Add(new FacilityViewModel(facil));
}
viewModel.Facilities = FacilityCollection;
}
查看:
<fieldset>
<legend>Facility</legend>
<div class="editor-field">
@Html.EditorFor(x => x.Facilities)
</div>
</fieldset>
现在一切似乎都运作良好,我希望这有助于其他人!
答案 0 :(得分:1)
问题是,当您应该使用编辑器模板时,您似乎正在使用部分视图。使用Partials时,您需要传递完整的Model,以便Html Helpers在其生成的名称/ ID中保留完整的对象层次结构。由于Model Binder在反序列化期间使用这些名称,如果层次结构丢失......数据无法重新饱和。
我建议使用编辑模板,这样可以让你的部分视图中的Razor代码保持相同,同时只传递顶级模型中所需的对象......在你的情况下是列表。
以下是关于如何使用它们的非常好的教程:http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/