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

Razor Pages中OnPost方法内BindProperty标记List返回Null问题

ASP.NET Core Razor Pages中List绑定为Null的解决方法

问题描述

在ASP.NET Core Razor Pages开发中,标记了[BindProperty]的List<BranchInfo>类型字段updatedData,调用OnPostAddBranch方法时返回Null。页面可正常遍历展示23条数据,但提交后无法完成绑定。


相关代码

PageModel代码

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ProfileMaker.Classes;
using System.Security.Claims;

namespace ProfileMaker.Pages
{
    public class AdminBranchDetailsModel : PageModel
    {
        public List<BranchInfo> BranchContactDetails { get; set; } = new List<BranchInfo>();
        [BindProperty]
        public List<BranchInfo> updatedData { get; set; } = new List<BranchInfo>();
        [BindProperty]
        public BranchInfo newBranch { get; set; } = new BranchInfo();

        public void OnGet()
        {
            BranchContactDetails = SetBranchData();
            updatedData = BranchContactDetails;
            Console.WriteLine(updatedData.Count());
        }
        public IActionResult OnPostAddBranch() 
        {
            Console.WriteLine(updatedData.Count());
            return RedirectToPage();
        }
    }
}

BranchInfo类代码

using Microsoft.EntityFrameworkCore;

namespace ProfileMaker.Classes
{
    public class BranchInfo 
    {
        public string whsNumber;
        public string whsName;
        public string whsAddress;
        public string whsCity;
        public string whsState;
        public string whsZip;
        public string whsDivision;
        public string whsGM;
        public string whsCountry;
        public string whsTimeZone;
        public int Index;
    }
}

cshtml页面代码

@page
@model ProfileMaker.Pages.AdminBranchDetailsModel
@{
    @if(Model.bIsAdmin)
    {
        <form method="post">
            <table>
                <tr>
                    <td>Warehouse Number</td>
                    <td>Warehouse Address</td>
                    <td>Warehouse City</td>
                    <td>Warehouse State/Province</td>
                    <td>Warehouse Zip/Postal</td>
                    <td>Warehouse Division</td>
                    <td>Warehouse General Manager</td>
                    <td>Warehouse Country</td>
                    <td>Warehouse Timezone</td>
                </tr>
                @for (int i = 0; i < Model.updatedData.Count(); i++)
                {
                <tr>
                    <td><input type="text" asp-for="updatedData[i].whsNumber"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsAddress" /></td>
                    <td><input type="text" asp-for="updatedData[i].whsCity"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsState"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsZip"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsDivision"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsGM"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsCountry"/></td>
                    <td><input type="text" asp-for="updatedData[i].whsTimeZone"/></td>
                    <td><button asp-page-handler="RemoveBranch" asp-route-index="@i">Remove</button></td>
                </tr>
                }
                <tr>
                    <td><input type="text" value="Num" asp-for="newBranch.whsNumber" /></td>
                    <td><input type="text" value="Address" asp-for="newBranch.whsAddress" /></td>
                    <td><input type="text" value="City" asp-for="newBranch.whsCity" /></td>
                    <td><input type="text" value="State/Prov" asp-for="newBranch.whsState" /></td>
                    <td><input type="text" value="Zip/Postal" asp-for="newBranch.whsZip" /></td>
                    <td><input type="text" value="Div" asp-for="newBranch.whsDivision" /></td>
                    <td><input type="text" value="GM" asp-for="newBranch.whsGM" /></td>
                    <td><input type="text" value="Country" asp-for="newBranch.whsCountry" /></td>
                    <td><input type="text" value="Time Zone" asp-for="newBranch.whsTimeZone" /></td>
                    <td><button asp-page-handler="AddBranch">Add</button></td>
                </tr>
                <tr>
                    <td><button asp-page-handler="UpdateBranch">Update</button></td>
                </tr>
            </table>
        </form>
    }
    else
    {
        <h1>You do not have Admin permissions</h1>
    }
}

问题原因及解决方法

1. 核心问题:BranchInfo使用字段而非属性

ASP.NET Core模型绑定系统仅支持绑定到带有get/set访问器的属性,而当前BranchInfo类中定义的是公共字段(如public string whsNumber;),导致绑定系统无法识别这些成员,最终updatedData无法被填充。

修改BranchInfo类,将所有字段改为属性:

using Microsoft.EntityFrameworkCore;

namespace ProfileMaker.Classes
{
    public class BranchInfo 
    {
        public string WhsNumber { get; set; }
        public string WhsName { get; set; }
        public string WhsAddress { get; set; }
        public string WhsCity { get; set; }
        public string WhsState { get; set; }
        public string WhsZip { get; set; }
        public string WhsDivision { get; set; }
        public string WhsGM { get; set; }
        public string WhsCountry { get; set; }
        public string WhsTimeZone { get; set; }
        public int Index { get; set; }
    }
}

同时同步修改cshtml页面中的asp-for表达式(C#大小写敏感,需匹配属性名称):

@for (int i = 0; i < Model.updatedData.Count(); i++)
{
<tr>
    <td><input type="text" asp-for="updatedData[i].WhsNumber"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsAddress" /></td>
    <td><input type="text" asp-for="updatedData[i].WhsCity"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsState"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsZip"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsDivision"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsGM"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsCountry"/></td>
    <td><input type="text" asp-for="updatedData[i].WhsTimeZone"/></td>
    <td><button asp-page-handler="RemoveBranch" asp-route-index="@i">Remove</button></td>
</tr>
}
<!-- newBranch部分同步修改 -->
<tr>
    <td><input type="text" value="Num" asp-for="newBranch.WhsNumber" /></td>
    <td><input type="text" value="Address" asp-for="newBranch.WhsAddress" /></td>
    <td><input type="text" value="City" asp-for="newBranch.WhsCity" /></td>
    <td><input type="text" value="State/Prov" asp-for="newBranch.WhsState" /></td>
    <td><input type="text" value="Zip/Postal" asp-for="newBranch.WhsZip" /></td>
    <td><input type="text" value="Div" asp-for="newBranch.WhsDivision" /></td>
    <td><input type="text" value="GM" asp-for="newBranch.WhsGM" /></td>
    <td><input type="text" value="Country" asp-for="newBranch.WhsCountry" /></td>
    <td><input type="text" value="Time Zone" asp-for="newBranch.WhsTimeZone" /></td>
    <td><button asp-page-handler="AddBranch">Add</button></td>
</tr>

2. 确认List绑定的索引连续性

当前页面使用for循环生成连续索引,这是List模型绑定的正确方式(foreach无法生成连续索引会导致绑定失败),保持当前循环方式即可。

3. 验证BindProperty特性

updatedData已标记[BindProperty],该设置正确,无需修改。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 12:35:06