基本上我有一个编辑器视图,其中有几个字段和一个列表,位于admin - >老师 - >修改
File.cshtml
<fieldset>
<legend>@T("Details")</legend>
@Html.LabelFor(m => m.FirstName, T("First name"))
@Html.TextBoxFor(m => m.FirstName)
@Html.LabelFor(m => m.LastName, T("Last name"))
@Html.TextBoxFor(m => m.LastName)
@Html.LabelFor(m => m.Gender, T("Gender"))
@Html.DropDownListFor(m => m.Gender, genderSelectListItems, "Select gender...")
</fieldset>
<fieldset>
@{
var i = 0;
@Html.LabelFor(m => m.Availability)
@Html.DropDownListFor(m => m.Availability, daysSelectList, "Select day to add...")
<input type="button" id="addDay" value="Add" />
<ul id="daysList">
@foreach (var day in Model.Availability)
{
<li>
@Html.TextBoxFor(m => m.Availability[i].Interval)
</li>
i++;
}
</ul>
}
</fieldset>
每当我提交数据时,只会保存字段。即使fiddler显示数据已发送回服务器
,也永远不会保存该列表我的模特是:
的 TeacherPartRecord
public class TeacherPartRecord : ContentPartRecord
{
public TeacherPartRecord()
{
TeacherData = new List<TeacherDataRecord>();
}
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual Gender Gender { get; set; }
public virtual IList<TeacherDataRecord> TeacherData { get; set; }
}
TeacherPart - 此文件与TeacherPartRecord几乎相同,因此我将其截断,仅显示差异
public class TeacherPart : ContentPart<TeacherPartRecord>
{
................
public IList<AvailabilityRecord> Availability
{
get { return Record.TeacherData.Select(r => r.AvailabilityRecord).ToList(); }
}
}
TeacherDataRecord
public class TeacherDataRecord
{
public virtual int Id { get; set; }
public virtual TeacherPartRecord TeacherPartRecord { get; set; }
public virtual AvailabilityRecord AvailabilityRecord { get; set; }
}
AvailabilityRecord
public class AvailabilityRecord
{
public virtual int Id { get; set; }
public virtual Day Day { get; set; }
public virtual string Interval { get; set; }
}
和 TeacherPartDriver
public class TeacherPartDriver : ContentPartDriver<TeacherPart>
{
//GET
protected override DriverResult Editor(TeacherPart part, dynamic shapeHelper)
{
return ContentShape("Parts_Teacher_Edit", () =>
shapeHelper.EditorTemplate(TemplateName: "Parts/Teacher", Model: BuildEditorViewModel(part), Prefix: Prefix));
}
//POST
protected override DriverResult Editor(TeacherPart part, IUpdateModel updater, dynamic shapeHelper)
{
var viewModel = new TeacherEditViewModel();
updater.TryUpdateModel(viewModel, Prefix, null, null);
_teacherService.UpdateTeacher(viewModel, part);
return Editor(part, shapeHelper);
}
}
BuildEditorViewModel 仅将 TeacherPart 中的属性映射到 TeacherEditViewModel ,这与 TeacherPart
相同POST编辑器方法永远不会从可用性接收数据( part.Availability 与以前相同,未收到新数据)
我手动在数据库中插入了一些条目,因为即使创建了其他内容(名字,姓氏,性别),我也无法从编辑页面添加类型可用性列表
我想知道绑定是如何/何时发生的,或者可能是我如何解决这个问题的一些建议。
我应该提一下,我的c#/ MVC体验是6个月,我的果园经验大约是2周,所以我还在学习。 我所希望的是,我清楚地说明了我的问题,并且有人可以提供帮助。 谢谢你的时间
修改 正如我所看到的,值是绑定的
<input id="Teacher_Availability_0__Interval" name="Teacher.Availability[0].Interval" type="text" value="14-20">
<input id="Teacher_Availability_1__Interval" name="Teacher.Availability[1].Interval" type="text" value="12-18">
和fiddler显示数据正在发送
所以我想在发送数据之后和服务器接收之前发生了错误,或者我的模型错了