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

4

我有一个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

1
你提交的表单标签中是否包含隐藏字段? - Karthik Ganesan
@lizzy91 请编辑您的问题,包括一个[mcve],可以由其他人编译和测试。 您缺少一些类,例如控制器和“EntityDto”类。 另外,获取并输出您在控制器中接收到的请求表单参数名称,以查看浏览器发送了什么。 - Progman
3
请移除自定义的 name 属性。ASP.NET 会为模型绑定生成它自己的 name 属性值。 - Dai
1
类似于<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 my Department 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
显示剩余4条评论
2个回答

5

经过3天的努力,我终于找到了解决方案。

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

我在我的InputModel上添加了DataAttribute HiddenInput。那应该是一样的工作方式。为什么不是呢? - liqSTAR

0

注意:表单中提交的数据模型类型必须与 post 方法中的参数类型相同。

输入框中 asp-for 的值表示 View 中的 @model 应该是 CreateOrEditStoreWalkViewModel,但从您的截图中可以看出,方法接收的参数类型是 CreateOrEditStoreWalkDto

尝试像下面的示例一样更改操作代码:

 [HttpPost]
    public void CreateStoreWalk(CreateOrEditStoreWalkViewModel createOrEditStoreWalkDto)
    {
        //the stuff you want
    }

我的代码那一部分其实是正确的。我的 Post 是通过一个动态 Web API 发送到一个 ApplicationService 而不是控制器,因此截图中大多数值都被正确设置。问题所在的属性是 Department,它是一个类。我似乎只有在处理类时会出现问题,但我相信这并不是任何限制。 - highboi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接