BindProperty 返回 NULL OnPost

问题描述 投票:0回答:1

我正在使用的列表上的 BindProperty 得到空返回。我将代码精简为仅相关的内容。当我调用 OnPost 方法 updateData 为 null 时。

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

它成功地迭代了 23 个条目并正确显示它们。

c# asp.net-core razor razor-pages
1个回答
0
投票

请使用属性而不是字段。

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; }
}

目标 | ASP.NET Core 中的模型绑定

模型绑定尝试查找以下类型目标的值:

  • 控制器或 PageModel 类的公共属性(如果由属性指定)。
© www.soinside.com 2019 - 2024. All rights reserved.