我有一个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
我的View.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>
我希望department.id
已经设置,但是department
总是为空。
![enter image description here](https://istack.dev59.com/q4DVD.webp)
name
属性。ASP.NET 会为模型绑定生成它自己的name
属性值。 - Dai<input asp-for="StoreWalk.Department.Id" type="hidden" />
这样的东西应该没有问题。但是你尝试的大部分其他内容都是错误的,肯定不会起作用。换句话说,name属性实际上应该最终变成像name="StoreWalk.Department.Id"
这样的东西...name="department_id"
完全不正确。 - Chris Pratt<input asp-for="StoreWalk.Department.Id" type="hidden" />
generated the following output, which also looks correct to me (thanks for confirming), but myDepartment
class is still null. Output:<input type="hidden" data-val="true" data-val-required="The Id field is required." id="StoreWalk_Department_Id" name="StoreWalk.Department.Id" value="8">
- highboi