You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 8 MVC中TenantId模型绑定始终为0的问题

ASP.NET Core 8 MVC:TenantId模型绑定始终为0的问题

代码实现

Action方法

// GET方法
public IActionResult TenantTBMapping(int tenantId)

// POST方法
[HttpPost]
public IActionResult TenantTBMapping(TenantTBMappingViewModel model)

模型类

public class TenantTBMappingViewModel
{
    public int TenantId { get; set; }
    public List<MappingConfigDetail> Details { get; set; }
    public List<string>? ToNames { get; set; }
    public List<SelectListItem>? Inputs { get; set; }
    public List<SelectListItem>? MappingTables { get; set; }
}

表单代码

@using (Html.BeginForm("TenantTBMapping", "Settings", FormMethod.Post))
{
    @Html.HiddenFor(model => model.TenantId);
    // 循环生成Details列表的编辑字段
    // 调用接收EditConfiguration列表的Edit组件生成可编辑项
}

问题现象

表单提交后,TenantTBMappingViewModel中的TenantId始终为0,但通过var test = Request.Form["TenantId"];能获取到正确数值,说明问题出在模型绑定环节。

若删除表单中除TenantId隐藏字段外的所有内容,TenantId可正常绑定。推测是表单内的Details循环字段或Edit组件生成的内容干扰了绑定,尽管这些字段自身绑定正常。

请求负载说明

提交的表单负载包含:

  • 值正确的TenantId字段
  • Details列表的各项子字段
  • Edit组件生成的相关表单字段

内容的提问来源于stack exchange,提问作者Caleb Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:48:21