【问题标题】:is there a limit on how many input tags can be used in razor pages剃刀页面中可以使用多少输入标签是否有限制
【发布时间】:2022-01-06 07:08:34
【问题描述】:

我正在尝试创建一个编辑页面,在单击按钮时更改公司/位置名称 我正在使用以下代码来存储所选数据以防验证失败:

@for (int i = 0; i < Model.Data.Locations.Count; i++)
    {
        <input type="hidden" asp-for="Data.Locations[i].Id" name="LocationId(@i)"/>
        <input type="hidden" asp-for="Data.Locations[i].Name" name="LocationName(@i)"/>
        <input type="hidden" asp-for="Data.Locations[i].CompanyId" name="LocationCompId(@i)"/>
    }

这会导致错误 400 请求,所有值似乎都在那里。 最奇怪的是,如果我使用这些输入标签中的任何两个,则该值被正确提取,只有当所有 3 个都放在那里时,它才起作用。 companyId 输入是以下 DTO 的外键:

@for (int i = 0; i < Model.Data.Companies.Count; i++)
    {
            <input type="hidden" asp-for="Data.Companies[i].Id" name="CompanyId(@i)"/>
        <input type="hidden" asp-for="Data.Companies[i].Name" name="CompanyName(@i)"/>
    }

c# DTO 的布局如下:

public List<CompanyDto> Companies { get; init; }
public List<LocationDto> Locations { get; init; }
           

            public record CompanyDto
            {
                public int Id { get; init; }
                public string Name { get; init; }
            }

            public record LocationDto
            {
                public int Id { get; init; }
                public string Name { get; init; }
                public int CompanyId { get; init; }
            }

【问题讨论】:

  • 你能显示你的动作参数吗?您的姓名属性可能不正确。
  • 400 错误可能是缺少防伪令牌。或者 GET url 太长,在这种情况下你可以 POST 表单。
  • 虽然也许你应该考虑将数据序列化为 json...

标签: c# html asp.net-core razor


【解决方案1】:

.net core 用name属性绑定数据,asp-for会生成带有idname的html,你可以去掉你代码中的name属性(你名字的格式不对) .这是一个将数据从视图传递到处理程序的演示:

public class Data
    {
        public List<CompanyDto> Companies { get; set; }
        public List<LocationDto> Locations { get; set; }
    }

cshtml.cs:

[BindProperty]
        public Data Data { get; set; } = new Data();
        public void OnGet()
        {
            Data.Companies= new List<CompanyDto> { new CompanyDto { Id = 1, Name = "c1" }, new CompanyDto { Id = 2, Name = "c2" }, new CompanyDto { Id = 3, Name = "c3" } };
            Data.Locations = new List<LocationDto> { new LocationDto { Id = 11, CompanyId = 1, Name = "l1" }, new LocationDto { Id = 22, CompanyId = 2, Name = "l2" }, new LocationDto { Id = 33, CompanyId = 3, Name = "l3" } };
        }
        public IActionResult OnPost()
        {
            return Page();
        }

cshtml:

<form method="post">
    @for (int i = 0; i < Model.Data.Locations.Count; i++)
    {
        <input asp-for="Data.Locations[i].Id" />
        <input asp-for="Data.Locations[i].Name" />
        <input asp-for="Data.Locations[i].CompanyId" />
    }
    @for (int i = 0; i < Model.Data.Companies.Count; i++)
    {
        <input asp-for="Data.Companies[i].Id" />
        <input asp-for="Data.Companies[i].Name" />
    }
    <input type="submit" value="submit" />
</form>

结果:

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2012-02-19
    • 1970-01-01
    相关资源
    最近更新 更多