我有我通過模型的局部視圖,它顯示checkboxs用戶可以選擇,這最終將得到與我的形式發佈,這樣我可以查詢自己選中的狀態,並根據需要將數據插入到數據庫中。MVC3數據綁定
我想改變我的局部視圖,而不是採取包含的機型列表的模型,因此它需要的型號列表直接由此解耦,並使得作爲局部視圖變得更有用。問題是,當我這樣做時,它似乎沒有正確綁定數據。下面是代碼之前的例子(正常工作和不工作之後)。
我非常感謝,如果有人能夠闡明爲什麼我的更改沒有影響,即在後代碼時,當我檢查發佈的數據列表爲空和在它正確填充之前:
前:
呼叫的局部視圖:
<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][email protected], Model.Facilities[i].name);
@Html.CheckBoxFor(
x => x.Facilities[i][email protected],
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)[email protected], Model.ElementAt(i).name);
@Html.CheckBoxFor(
x => x.ElementAt(i)[email protected],
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>
一切似乎現在的工作很好,我希望這可以幫助別人!
什麼是'ElementAt'?那真的是你的代碼嗎? – 2013-02-15 13:50:25
是的,ElementAt應該讓我在索引位置的元素,這應該是該特定循環項目的設施? – Yos 2013-02-15 14:35:07
只要做'x => x。[i] .name'等,看看會發生什麼 – 2013-02-15 14:38:54