gpt4 book ai didi

c# - Entity Framework |错误 : InvalidCastException: The field of type System. Int32 必须是字符串、数组或 ICollection 类型

转载 作者:行者123 更新时间:2023-12-03 18:40:30 25 4
gpt4 key购买 nike

问题

当我向 Home Controller 提交(或发布)一个名为 NewsletterSignup 的方法的请求时,我收到以下错误。在提交我的时事通讯表单之后,在调用此方法并创建捕获表单数据的对象之前,会提示错误,从而造成难以排除故障的情况,并确定此确切问题在何处传播。

Error

An unhandled exception occurred while processing the request.

InvalidCastException: The field of type System.Int32 must be a string, array or ICollection type. ... System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid(object value)

... System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(object value, ValidationContext validationContext)

... System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(object value, ValidationContext validationContext)



文件

AppDbContext.cs

Application Database Context



namespace KingsEye.Data
{
public class AppDbContext : IdentityDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

public DbSet<Home> Home { get; set; }
public DbSet<Pages> Pages { get; set; }
public DbSet<Newsletter> Newsletter { get; set; }
}
}

CollectionModel.cs

View Model used on index.cshtml and shared newsletter.cshtml view



namespace KingsEye.Models
{
public class CollectionModel
{
public Home Home { get; set; }
public Pages Pages { get; set; }
public Newsletter Newsletter { get; set; }
}
}

Newsletter.cs

Newsletter View Model



namespace KingsEye.Models
{
public class Newsletter
{
#region Newsletter
[Display(Name = "Newsletter ID: ")]
[Required(ErrorMessage = "Newsletter ID Required!")]
public int Id { get; set; }

[Display(Name = "Full Name: ")]
[DataType(DataType.Text)]
public string Fname { get; set; }

[Display(Name = "E-Mail Address: ")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }

[Display(Name = "Phone Number: ")]
[DataType(DataType.PhoneNumber)]
public int Phone { get; set; }

[Display(Name = "Active: ")]
[MaxLength(1)]
public int Active { get; set; }

[Display(Name = "GUID: ")]
[MaxLength(37)]
public string GUID { get; set; }

[Display(Name = "Created: ")]
[DataType(DataType.DateTime)]
public DateTime Create { get; set; }

[Display(Name = "Updated: ")]
[DataType(DataType.DateTime)]
public DateTime Update { get; set; }
#endregion
}
}

HomeController.cs

Home Controller



[HttpPost]
public async Task<IActionResult> NewsletterSignup(CollectionModel model)
{
<= Error: is prompting prior to the body of this method being called,
and the parameter object being populated with post data!!!

var newsletter = new Newsletter
{
Id = 0,
Fname = model.Newsletter.Fname,
Email = model.Newsletter.Email,
Phone = model.Newsletter.Phone,
Active = 0,
GUID = Guid.NewGuid().ToString(),
Create = DateTime.Now,
Update = DateTime.Now
};

...
}

_Newsletter.cshtml

Shared view for the newsletter form


@model CollectionModel

<div id="newsletter">

@if (User.Identity.IsAuthenticated)
{
<header>
<i class="far fa-envelope-open"></i>
<div class="text">Newsletter</div>
<i class="far fa-envelope-open"></i>
</header>

<main>Subscribe to our newsletter</main>

<footer>
<form asp-controller="Home" asp-action="NewsletterSignup" method="post" id="newsletter-form" class="text-danger input-form">
<div asp-validation-summary="ModelOnly"></div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Id" type="hidden" value="0" />
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Fname" id="email-signed" class="form-control" placeholder="Your Name" />
<span asp-validation-for="@Model.Newsletter.Fname"></span>
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Email" type="hidden" value="place@holder.com" />
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Phone" id="email-signed" class="form-control" placeholder="(000) 000-0000" />
<span asp-validation-for="@Model.Newsletter.Phone"></span>
</div>

<div class="form-group input-submit">
<input id="newsletter-subscribe" type="submit" value="Subscribe" class="btn material-button" />
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Active" type="hidden" value="0" />
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.GUID" type="hidden" value="0" />
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Create" type="hidden" value="0" />
</div>

<div class="form-group">
<input asp-for="@Model.Newsletter.Update" type="hidden" value="0" />
</div>

</form>
</footer>
}
else
{
<main>
Please <a asp-controller="Auth" asp-action="Register">Sign-Up</a> to Subscribe for a Newsletter!
</main>
}
</div>

目标

想确定此错误是从哪里传播的,以便可以相应地调试有问题的异常。任何建议或方向肯定会受到赞赏。
  • 提前致谢和欢呼。
  • 最佳答案

    MaxLength 属性用于字符串

        [Display(Name = "Active: ")]
    [MaxLength(1)] // this will fail
    public int Active { get; set; }

    [Display(Name = "GUID: ")]
    [MaxLength(37)] //this is okay
    public string GUID { get; set; }
    https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.maxlengthattribute?view=netframework-4.8

    Gets the maximum allowable length of the array or string data.

    关于c# - Entity Framework |错误 : InvalidCastException: The field of type System. Int32 必须是字符串、数组或 ICollection 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476819/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com