hig*_*boi 4 c# asp.net-core-mvc asp.net-core
我有一个带有嵌套类的 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" />
Run Code Online (Sandbox Code Playgroud)
我的模特班
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; }
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
3天,我终于找到了解决方案。
<input type="hidden" name="department[id]" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" name="department[name]" value="@Model.StoreWalk.Department.Name" />
Run Code Online (Sandbox Code Playgroud)