C# 自定义EditorTemplate未正确返回表单值
我有一个属性为复杂类型的模型。我已经为复杂的子类型创建了一个自定义的DisplayEditor,并且在加载页面时它被正确绑定。编辑后发布页面时,Dependents类型将设置为null。以下是表示子Dependents属性的员工模型的代码:C# 自定义EditorTemplate未正确返回表单值,c#,.net,asp.net-mvc,asp.net-mvc-5,C#,.net,Asp.net Mvc,Asp.net Mvc 5,我有一个属性为复杂类型的模型。我已经为复杂的子类型创建了一个自定义的DisplayEditor,并且在加载页面时它被正确绑定。编辑后发布页面时,Dependents类型将设置为null。以下是表示子Dependents属性的员工模型的代码: [Display(Name = "Dependents")] [DataType(DataType.MultilineText)] public List<Dependent> Dependents { get; set; } 员工控制器上的两
[Display(Name = "Dependents")]
[DataType(DataType.MultilineText)]
public List<Dependent> Dependents { get; set; }
员工控制器上的两种编辑操作方法(我尝试过TryUpdateModel,不起作用):
公共视图结果编辑(int-employeeId)
{
如果(employeeId<0)抛出新ArgumentOutOfRangeException(nameof(employeeId));
Employee=\u employeeRepository.Employees.FirstOrDefault(e=>e.EmployeeId==EmployeeId);
bool result=TryUpdateModel(员工,新表单值提供者(ControllerContext));
返回视图(员工);
}
[HttpPost]
公共操作结果编辑(员工)
{
如果(employee==null)抛出新的ArgumentNullException(nameof(employee));
if(ModelState.IsValid)
{
employee.Changed=true;
employee.Dependents.ForEach(d=>d.Changed=true);
_employeeRepository.SaveEmployee(employee);
TempData[“message”]=$“{employee}已保存。”;
返回操作(“索引”);
}
否则{
//数据值有问题
返回视图(员工);
}
}
以下是Edit.cshtml:
@model Paylocity.HR.Domain.Entities.Employee
@{
ViewBag.Title = $"{"Edit"} {Model}";
}
<div class="panel panel-default">
<div class="panel-heading">
<h3>@ViewBag.Title</h3>
</div>
@using (Html.BeginForm("Edit", "Employee"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr/>
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
<h4>Employee</h4>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.FirstName, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.LastName, "", new {@class = "text-danger"})
</div>
</div>
<hr/>
@Html.EditorFor(model => model.Dependents, "Dependents")
@Html.HiddenFor(model => model.EmployeeId)
</div>
<div class="panel-footer">
<input type="submit" value="Save" class="btn btn-primary"/>
@Html.ActionLink("Cancel and return to List", "Index", null, new {@class = "btn btn-default"})
</div>
}
</div>
@model Paylocity.HR.Domain.Entities.Employee
@{
ViewBag.Title=$“{”编辑“}{Model}”;
}
@视图包。标题
@使用(Html.BeginForm(“编辑”、“员工”))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
雇员
@LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.LastName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”})
@EditorFor(model=>model.Dependents,“Dependents”)
@Html.HiddenFor(model=>model.EmployeeId)
@ActionLink(“取消并返回列表”,“索引”,null,新建{@class=“btn btn default”})
}
以下是Dependent.cshtml编辑器模板:
@model IEnumerable<Dependent>
@using Paylocity.HR.Domain.Entities
@foreach (var dep in Model)
{
<h4>Dependent</h4>
<div class="form-group">
@Html.LabelFor(m => dep.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => dep.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => dep.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => dep.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => dep.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => dep.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => dep.DependentType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(m => dep.DependentType, new { @class = "form-control" })
@Html.ValidationMessageFor(m => dep.DependentType, "", new { @class = "text-danger" })
</div>
</div>
<hr />
}
@model IEnumerable
@使用Paylocity.HR.Domain.Entities
@foreach(模型中的var dep)
{
依赖的
@LabelFor(m=>dep.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(m=>dep.FirstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(m=>dep.FirstName,“,new{@class=“text danger”})
@LabelFor(m=>dep.LastName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(m=>dep.LastName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>dep.LastName,“,new{@class=“text danger”})
@LabelFor(m=>dep.DependentType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EnumDropDownListFor(m=>dep.DependentType,新的{@class=“form control”})
@Html.ValidationMessageFor(m=>dep.DependentType,“,new{@class=“text danger”})
}
employee对象绑定正确且可更新,只有dependents子类型未正确绑定。HTML显示了相关表单字段的正确ID/名称(我相信?)。我是否需要实现某种自定义绑定器代码,或者我在这里遗漏了一些明显的东西
这是我的第一个问题,希望我提供了足够的信息。我认为您的问题在于生成项目列表的方式 使用索引而不是foreach 因此,您的Dependent.cshtml编辑器模板 做一些类似于:
@for(int i = 0; i < Model.Count(); i++)
{
<h4>Dependent</h4>
<div class="form-group">
@Html.LabelFor(m => Model[i].FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => Model[i].FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => Model[i].FirstName, "", new { @class = "text-danger" })
</div>
</div>
// rest field follow same pattern
}
for(int i=0;i我认为您的问题在于生成项目列表的方式 使用索引而不是foreach 因此,您的Dependent.cshtml编辑器模板 做一些类似于:
@for(int i = 0; i < Model.Count(); i++)
{
<h4>Dependent</h4>
<div class="form-group">
@Html.LabelFor(m => Model[i].FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => Model[i].FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => Model[i].FirstName, "", new { @class = "text-danger" })
</div>
</div>
// rest field follow same pattern
}
for(int i=0;i将
Dependent.cshtml
模板中的模型更改为@model Dependent
(它不能是IEnumerable
),并删除foreach
循环,该循环正在生成与模型无关的名称
属性(以及重复的id
属性,该属性是无效的html)
它也需要b
@for(int i = 0; i < Model.Count(); i++)
{
<h4>Dependent</h4>
<div class="form-group">
@Html.LabelFor(m => Model[i].FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => Model[i].FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => Model[i].FirstName, "", new { @class = "text-danger" })
</div>
</div>
// rest field follow same pattern
}
@model Dependent
...
@Html.EditorFor(m => m.FirstName, new { htmlAttributes = new { @class = "form-control" } })
...
@Html.EditorFor(m => m.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.Dependents)