gpt4 book ai didi

c# - 尽管模型有效,但 TryValidateModel 仍返回 false

转载 作者:行者123 更新时间:2023-12-04 15:04:25 27 4
gpt4 key购买 nike

我有一个 Controller ,它使用模型绑定(bind)从表单中检索数据。我需要在验证之前对模型进行一些编辑,所以我按照文档建议使用 TryValidateModel。但是由于某种原因,即使“用户”不为空,“作者”也总是无效的。真的很困惑这里发生了什么,任何帮助将不胜感激。
显示有效模型的调试器:
valid model
Controller :

    [Authorize]
[HttpPost]
public async Task<IActionResult> Create([Bind("Id, Title, Content")] Post post)
{
var user = await userManager.GetUserAsync(User);
post.Author = user;
post.UpvotedBy.Add(user);

if (post.Title != null && post.Content != null)
{
post.Title = post.Title.Trim();
post.Content = post.Content.Trim();
}

if (TryValidateModel(post)) // Never true
{
context.Add(post);
context.SaveChanges();

return RedirectToAction("Details", new { id = post.Id });
}
else return View(post);
}
模型:
public abstract class Entry
{
public Entry()
{
DateCreated = DateTime.Now;
Upvotes = 1;
Downvotes = 0;
VoteScore = 1;

Replies = new List<Comment>();
SavedBy = new List<ApplicationUser>();
HiddenBy = new List<ApplicationUser>();
UpvotedBy = new List<ApplicationUser>();
DownvotedBy = new List<ApplicationUser>();
}

public int Id { get; set; }

[Required, StringLength(40000)]
public string Content { get; set; }

[Required, DataType(DataType.DateTime)]
public DateTime DateCreated { get; set; }

[Required]
public int Upvotes { get; set; }
[Required]
public int Downvotes { get; set; }
[Required]
public int VoteScore { get; set; }

[Required]
public ApplicationUser Author { get; set; }
[Required]
public ICollection<Comment> Replies { get; set; }
[Required]
public ICollection<ApplicationUser> SavedBy { get; set; }
[Required]
public ICollection<ApplicationUser> HiddenBy { get; set; }
[Required]
public ICollection<ApplicationUser> UpvotedBy { get; set; }
[Required]
public ICollection<ApplicationUser> DownvotedBy { get; set; }
}

public class Post : Entry
{
[Required, StringLength(300, MinimumLength = 1)]
public string Title { get; set; }
}

形式:
@model Post
@{
ViewData["Title"] = "New post";
}

<div class="container">
<form asp-action="Create">
<div class="form-group mb-3">
<h3>Title</h3>
<input asp-for="Title" class="form-control bg-dark text-white border-secondary"
placeholder="Make it descriptive!"/>
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group mb-3">
<h3>Content</h3>
<textarea asp-for="Content" class="form-control bg-dark text-white border-secondary"
placeholder="What do you have to say?" rows="5"></textarea>
<span asp-validation-for="Content" class="text-danger"></span>
</div>

<a href="javascript:history.go(-1)" class="btn btn-outline-danger" role="button">
<i class="fas fa-ban me-1"></i> Cancel
</a>
<button type="submit" class="btn btn-outline-primary">
<i class="fas fa-save me-1"></i> Submit
</button>
</form>
</div>

最佳答案

尝试清零 ModelState调用前 TryValidate :

// your model update code
ModelState.Clear();
if (TryValidateModel(post))
{
....
}
另一种选择是实现 custom model binder绑定(bind) Author来自 UserManager在验证发生之前。

关于c# - 尽管模型有效,但 TryValidateModel 仍返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66429209/

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