Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Dto class with nested classes im using to bind a model to my view. The nested class has an id property that needs to be passed back into my app service, but so far im getting null

Some things i've tried are

<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" />

My model class

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 can be found here: https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Application/Services/Dto/EntityDto.cs

My 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>

I expect the department.id to be set but department is always null. enter image description here

like image 756
highboi Avatar asked Oct 27 '25 06:10

highboi


1 Answers

3 days and I finally figured out a solution.

<input type="hidden" name="department[id]" value="@Model.StoreWalk.Department.Id" />
<input type="hidden" name="department[name]" value="@Model.StoreWalk.Department.Name" />
like image 130
highboi Avatar answered Oct 28 '25 22:10

highboi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!