簡體   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