gpt4 book ai didi

c# - 为什么我的隐藏输入没有在模型类中传递?

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:44 25 4
gpt4 key购买 nike

我有一个带有嵌套类的 Dto 类,我用它来将模型绑定(bind)到我的 View 。嵌套类有一个 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.iddepartment 始终为空。 enter image description here

最佳答案

3 天,我终于找到了解决方案。

<input type="hidden" name="department[id]" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" name="department[name]" value="@Model.StoreWalk.Department.Name" />

关于c# - 为什么我的隐藏输入没有在模型类中传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54153230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com