【问题标题】:Why are my hidden input not being passed in the model class?为什么我的隐藏输入没有在模型类中传递?
【发布时间】: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>

我希望设置 department.id,但 department 始终为空。

【问题讨论】:

  • 您提交的表单标签中的隐藏字段是否返回?
  • @KarthikGanesan 是,我已经用我的视图代码更新了我的问题
  • @lizzy91 请编辑您的问题以包含一个minimal reproducible example,它可以被其他人编译和测试。您缺少几个类,例如控制器和 EntityDto 类。还要获取并输出您在控制器中收到的请求表单参数名称,以查看浏览器发送的内容。
  • 删除您的自定义 name 属性。 ASP.NET 为模型绑定生成自己的name 属性值。
  • &lt;input asp-for="StoreWalk.Department.Id" type="hidden" /&gt; 之类的东西应该没有问题。但是,您尝试过的其他大多数事情都被破坏了,而且肯定根本不起作用。换句话说,name 属性实际上应该以name="StoreWalk.Department.Id"...name="department_id" 之类的形式结束,这根本不对。

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

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。这应该以同样的方式工作。为什么没有?
【解决方案2】:

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

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

尝试像下面的演示那样更改您的操作代码

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

【讨论】:

  • 我的那部分代码实际上是正确的。我的Post 正在使用动态 Web api 而不是控制器发送到ApplicationService,因此大多数值都在屏幕截图中正确设置。有问题的属性是Department,这是一个类。我似乎只对课程有疑问,但我相信这不是任何形式的限制。
猜你喜欢
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
相关资源
最近更新 更多