asp.net-mvc - Aspnet Core MVC 验证消息始终可见
问题描述
我正在实现表单验证,我的模型如下所示:
public class Profile
{
[Required]
public string Firstname { get; set; }
}
我的观点是这样的:
<form asp-action="SaveProfile" asp-controller="User" asp- profile="@Model" asp-file="file" enctype="multipart/form-data" method="POST">
<label asp-for="Firstname">Firstname</label>
<input class="form-control" id="firstname" asp-for="Firstname" placeholder="Firstname">
<span asp-validation-for="Firstname"></span>
<input class="form-control" type="submit" value="Save">
</form>
我的控制器:
public async Task<IActionResult> Index(Profile profile)
{
return View(await _userRepository.ReadProfile(User.Identity.Name));
}
和ReadProfile()
:
public async Task<Profile> ReadProfile(string user)
{
var profile = await _context.User
.Where(u => u.Email == user)
.Include(s => s.Schedule)
.Include(s => s.Skills)
.FirstOrDefaultAsync() ?? await CreateProfile(user);
var schedule = profile.Schedule.OrderBy(sd => sd.StartDate).ToList();
profile.Schedule = schedule;
return profile;
}
当我加载视图时,Firstname is required
即使该字段包含提交数据,我也会看到
有任何想法吗?
解决方案
推荐阅读
- vue.js - Vue.js 使用 @input 事件发送索引
- javascript - 如何将 PHP 变量传递给 JavaScript?
- r - 如何删除 spplot 中的 zcol 框?
- git - 将分支从另一个远程分支合并到我的分支后,git pull no tracking info for my branch
- java - 在 GridLayout 中交换组件时出现 IndexOutOfBoundException 和 NullPointerException?
- angular - Angular - 如何使用户角色复选框动态化
- react-native - 在 react-native Flatlist 或 scrollview 中,我们可以像 carousal 一样显示循环吗?
- angular8 - 如何在 Angular 8 中更改“ng-block-ui”的背景颜色?
- android - 在点击视图上抛出 NoSuchMethodException
- oracle - 优化器对散列连接和完全外连接做了什么?