验证有效,但未显示消息
Posted
技术标签:
【中文标题】验证有效,但未显示消息【英文标题】:Validation works but message is not displayed 【发布时间】:2021-08-08 15:43:53 【问题描述】:我正在尝试在提交表单之前验证模型中的特定字段。它可以工作但没有显示消息,这是因为名称属性不匹配。
这是我的模型:
public class TaskList
[Key]
public int Id get; set;
[MaxLength(200, ErrorMessage = "Subject field can't have more than 250 characters.")]
[Required(ErrorMessage = "Subject field cannot be empty.")]
[Column(TypeName = "nvarchar")]
public string Subject get; set;
[MaxLength(1000, ErrorMessage = "Details field can't have more than 1000 characters.")]
[Column(TypeName = "nvarchar")]
public string Details get; set;
[Required(ErrorMessage = "Please select a module.")]
public TaskLabels Label get; set;
[Required]
[Column(TypeName = "date")]
public DateTime Created get; set;
[Column(TypeName = "date")]
public DateTime? DueDate get; set;
[Column(TypeName = "date")]
public DateTime? Completed get; set;
这是控制器动作:
[HttpPost]
public IActionResult TaskList(TaskList newTask)
_ticketRepo.AddTask(newTask);
return RedirectToAction("TaskList");
这是我的视图模型:
public class TaskListViewModel
public TaskList ToDo get; set;
public IEnumerable<TaskList> TasksList get; set;
这是我的表格:
<div class="modal fade" id="modal-add">
<div class="modal-dialog modal-lg">
<form class="form-horizontal" asp-controller="home" asp-action="TaskList" method="post">
<div class="modal-content">
<div class="modal-header bg-success">
<h4 class="modal-title">Add New To Do Item</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="card-body">
<div class="form-group row">
<label asp-for="ToDo.Subject" class="col-sm-2 col-form-label">Subject</label>
<div class="col-sm-10">
<input type="text" asp-for="ToDo.Subject" name="Subject" class="form-control" placeholder="Subject">
<span asp-validation-for="@Model.ToDo.Subject" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="ToDo.Details" class="col-sm-2 col-form-label">Details</label>
<div class="col-sm-10">
<textarea asp-for="ToDo.Details" name="Details" class="form-control" placeholder="Provide some details about the task."></textarea>
<span asp-validation-for="ToDo.Details" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="ToDo.Label" class="col-sm-2 col-form-label">Module</label>
<div class="col-sm-10">
<select name="ToDo.Label" asp-for="Label" asp-items="html.GetEnumSelectList<TaskLabels>()" class="form-control select2 select2-danger" data-dropdown-css-class="select2-danger" style="width: 100%;">
<option value="" selected="selected">Please select one</option>
</select>
<span asp-validation-for="ToDo.Label" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="ToDo.DueDate" class="col-sm-2 col-form-label">Date:</label>
<div class="col-sm-10 input-group date" id="duedate" data-target-input="nearest">
<input type="text" name="DueDate" asp-for="ToDo.DueDate" class="form-control datetimepicker-input" data-target="#duedate" />
<div class="input-group-append" data-target="#duedate" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div asp-validation-summary="All" class="text-danger"></div>
</div>
<!-- /.card-body -->
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</form>
</div>
<!-- /.modal-dialog -->
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script><script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
在我的表单中,如果我从输入字段中删除 name 属性,则验证有效,验证消息会显示,但模型绑定不起作用,并且我的属性出现空异常错误。我做错了什么?
【问题讨论】:
【参考方案1】:当然,我必须在我的控制器动作中实例化视图模型!当我这样做并从我的视图中的输入字段中删除所有名称属性时,一切正常!
[HttpPost]
public IActionResult TaskList(TaskListViewModel model)
if (ModelState.IsValid)
TaskList taskList = new TaskList
Created = DateTime.Now.Date,
Subject = model.ToDo.Subject,
Details = model.ToDo.Details,
DueDate = model.ToDo.DueDate,
Label = model.ToDo.Label
;
_ticketRepo.AddTask(taskList);
return RedirectToAction("TaskList");
else
return View();
【讨论】:
以上是关于验证有效,但未显示消息的主要内容,如果未能解决你的问题,请参考以下文章