如何在所有这些字段上设置验证消息?我不确定将所有内容直接绑定到我的实体模型取消时如何设置它吗?我尝试直接在我的entityclass中设置一个validationmessage,但不走运。
这是我的剃刀页
@page
@model Index
@{
ViewBag.Title = "Index";
}
<div class="body-content">
<form id="avboka-form" method="post">
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-med-5">
<label asp-for="Cancellation.Elev"></label>
<input type="text" id="elev" asp-for="Cancellation.Elev" class="form-control">
<span asp-validation-for="Cancellation.Elev"></span>
</div>
</div>
<div class="form-group">
<div class="col-med-5">
<label asp-for="Cancellation.Dag"></label>
<input asp-for="Cancellation.Dag" type="datetime" id="datepicker" class="datepicker1 form-control">
<span asp-validation-for="Cancellation.Dag"></span>
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => Model.SelectedKommun, htmlAttributes: new { @class = "control-label col-med-2" })
<div class="col-med-5">
@Html.DropDownListFor(x => Model.Cancellation.KommunId, new SelectList(Model.Kommun, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "kommun" })
@Html.ValidationMessageFor(x => x.SelectedKommun, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => Model.SelectedFordon, htmlAttributes: new { @class = "control-label col-med-2" })
<div class="col-med-5">
@Html.DropDownListFor(x => Model.Cancellation.FordonId, new SelectList(Model.Fordon, "Value", "Text"), htmlAttributes: new { @class = "form-control", @id = "fordon" })
@Html.ValidationMessageFor(x => x.SelectedFordon, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-med-5">
<label asp-for="Cancellation.Skola.Namn"></label>
<select id="skola" name="Cancellation.SkolaId" class="form-control">
@foreach (var schools in Model.School)
{
<option value="@schools.Id">@schools.Namn</option>
}
</select>
<span asp-validation-for="Cancellation.SkolaId"></span>
</div>
<input type="submit" name="save" value="Avboka skolskjuts" class="vt-btn" />
</form>
</div>
这里是我绑定输入字段的页面模型的一部分。选择是从其他表中收集的,因此永远不会为空。
[BindProperty]
public Avbokning Cancellation { get; set; }
public Index(SqlAvbokningData<Avbokning> avbokningRepo, SqlKommunData<Kommun> kommunRepo, SqlFordonData<Fordon> fordonRepo, SqlSkolaData<Skola> skolaRepo)
{
_avbokningRepo = avbokningRepo;
_kommunRepo = kommunRepo;
_fordonRepo = fordonRepo;
_skolaRepo = skolaRepo;
}
public async Task<IActionResult> OnGet()
{
Kommun = await _kommunRepo.GetKommuner();
Fordon = _fordonRepo.GetFordon();
Municipalities = await _kommunRepo.GetAll();
Vehicle = await _fordonRepo.GetAll();
School = await _skolaRepo.GetAll();
return Page();
}
[ValidateAntiForgeryToken]
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
//if (!Cancellation.Validate())
// return Redirect("/");
await _avbokningRepo.Add(Cancellation);
return Redirect("/Tack");
}
return RedirectToAction("OnGet");
}
答案 0 :(得分:0)
可以使用viewmodel完成MVC中的验证。您可以通过以下方式指定模型:
public class LogOnViewModel
{
[Required(ErrorMessage = "RequiredField")]
[Display(Name = "Username")]
public string Username { get; set; }
[Required(ErrorMessage = "RequiredField")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
}
一旦您进入网页本身,ValidationMessageFor便会根据您在视图模型上放置的数据批注来验证字段,并将其传递给网页。
在控制器中,您可以通过以下方式将其传递到页面上:
var viewModel = new LogOnViewModel();
// do stuff
return View(viewModel);
这不是一个完美的示例,但这应该使您对如何使用它有所了解。