- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Controller ,它使用模型绑定(bind)从表单中检索数据。我需要在验证之前对模型进行一些编辑,所以我按照文档建议使用 TryValidateModel。但是由于某种原因,即使“用户”不为空,“作者”也总是无效的。真的很困惑这里发生了什么,任何帮助将不胜感激。
显示有效模型的调试器:
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/
我有一个 Controller ,它使用模型绑定(bind)从表单中检索数据。我需要在验证之前对模型进行一些编辑,所以我按照文档建议使用 TryValidateModel。但是由于某种原因,即使“用户
我有一个需要模型列表的操作。我想同时验证每个模型与整个模型集合。我正在尝试使用 TryValidateModel,但似乎如果我的任何一个模型无效,那么所有模型都无效。我的表单显示 5 个 Survey
使用普通 MVC,我可以使用 TryValidateModel 重新验证我的模型。 TryValidateModel方法似乎不适用于WebAPI。使用 WebAPI 时如何重新验证我的模型? 最佳答案
我正在编写类似这样的测试框架:https://github.com/ivaylokenov/MyTested.WebApi但对于 ASP.NET 5 MVC 6(使用 6.0.0-beta8)。 我很
我有一个两步表单流程,其中第一组数据存储在 session 中。 [IsMp4File] [Required(ErrorMessage = "* Please select a video to up
我正在尝试为 Asp.Net Core Web API 的 ModelState 验证编写单元测试。 我读到,最好的方法是使用 TryValidateModel 函数。但是,每次我运行单元测试时,它都
我是一名优秀的程序员,十分优秀!