繁体   English   中英

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

[英]Why are my hidden input not being passed in the model class?

我有一个带有嵌套类的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; }
}

可以在此处找到EntityDtohttps//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总是空。 在此输入图像描述

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

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

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

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

尝试更改您的操作代码,如下面的演示

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM