【发布时间】:2019-01-16 00:26:30
【问题描述】:
我有一个带有嵌套类的 Dto 类,用于将模型绑定到我的视图。
嵌套类有一个 id 属性,需要传递回我的应用服务,但到目前为止我得到了 null
我尝试过的一些事情是
<input asp-for="StoreWalk.Department.Id" type="hidden" />
@Html.HiddenFor(h => h.StoreWalk.Department.Id)
<input type="hidden" name="version" value="@Model.StoreWalk.Version" />
<input type="hidden" name="department.id" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" name="department_id" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" id="StoreWalk_Department_Id" name="department_id" value="@Model.StoreWalk.Department.Id" />
我的模型课
public class CreateOrEditStoreWalkViewModel
{
public CreateOrEditStoreWalkDto StoreWalk { get; set; }
public bool IsEditMode => StoreWalk.Id.HasValue;
}
public class CreateOrEditStoreWalkDto : EntityDto<int?>
{
// Id property is implemented in `EntityDto` as int?
[Required]
public string Store { get; set; }
public string Comments { get; set; }
public byte[] Signature { get; set; }
public int Version { get; set; }
public DepartmentsDto Department { get; set; }
}
public class DepartmentsDto : EntityDto
{
// Id property is implemented in `EntityDto`
public string Name { get; set; }
}
EntityDto 可以在这里找到:https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Application/Services/Dto/EntityDto.cs
我的视图.cshtml
<form name="StoreWalkInformationsForm" role="form" novalidate class="form-validation">
@if (Model.IsEditMode)
{
<input type="hidden" name="id" value="@Model.StoreWalk.Id" />
}
// Note, im not trying all at once, these are just what ive tried so far
<input asp-for="StoreWalk.Department.Id" type="hidden" />
@Html.HiddenFor(h => h.StoreWalk.Department.Id)
<input type="hidden" name="version" value="@Model.StoreWalk.Version" />
<input type="hidden" name="department.id" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" name="department_id" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" id="StoreWalk_Department_Id" name="department_id" value="@Model.StoreWalk.Department.Id" />
.........
</form>
【问题讨论】:
-
您提交的表单标签中的隐藏字段是否返回?
-
@KarthikGanesan 是,我已经用我的视图代码更新了我的问题
-
@lizzy91 请编辑您的问题以包含一个minimal reproducible example,它可以被其他人编译和测试。您缺少几个类,例如控制器和
EntityDto类。还要获取并输出您在控制器中收到的请求表单参数名称,以查看浏览器发送的内容。 -
删除您的自定义
name属性。 ASP.NET 为模型绑定生成自己的name属性值。 -
<input asp-for="StoreWalk.Department.Id" type="hidden" />之类的东西应该没有问题。但是,您尝试过的其他大多数事情都被破坏了,而且肯定根本不起作用。换句话说,name 属性实际上应该以name="StoreWalk.Department.Id"...name="department_id"之类的形式结束,这根本不对。
标签: c# asp.net-core asp.net-core-mvc