![](/img/trans.png)
[英]Why the only hidden field that is being filled from GET action is not being passed in model?
[英]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; }
}
可以在此處找到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>
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.