Это простая страница формы. Фреймворк. NET Core 3.1, и эта страница в основном написана в соответствии с руководством Microsoft.
Модель
public class ProdoctModel
{
[Required]
[StringLength(100)]
public string product { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18, 2)")]
public decimal price { get; set; }
[Required]
public string locale { get; set; }
[Required]
public string category { get; set; }
public bool importGoods { get; set; }
}
Контроллер
public ActionResult Create()
{
var countries = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("en-US", "United States"), new KeyValuePair<string, string>("ch-CN", "China"), new KeyValuePair<string, string>("es-MX", "Mexico") };
var categories = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("1", "foods"), new KeyValuePair<string, string>("2", "3C"), new KeyValuePair<string, string>("3", "medicine") };
ViewBag.countries = new SelectList(countries, "Key", "Value");
ViewBag.categories = categories;
return View();
}
просмотр
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h3 class="text-center">
Add Product
</h3>
<form role="form" asp-controller="PrsExps" asp-action="Create" method="post">
<div class="form-group">
<label asp-for="product"></label>
<input asp-for="product" class="form-control" />
<span class="field-validation-valid text-danger" asp-validation-for="product" ></span>
</div>
<div class="form-group">
<label asp-for="price"></label>
<input asp-for="price" class="form-control" />
<span class="field-validation-valid text-danger" asp-validation-for="price"></span>
</div>
<div class="form-group">
<label asp-for="locale"></label>
<select asp-for="locale" asp-items="@ViewBag.countries">
<option>Please Select...</option>
</select>
<span class="field-validation-valid text-danger" asp-validation-for="locale"></span>
</div>
<div class="form-group">
<label asp-for="category"></label>
@foreach (var catItem in ViewBag.categories)
{
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" asp-for="category" id="category@(catItem.Key)" value="@catItem.Value">
<label class="form-check-label" asp-for="category">@catItem.Value</label>
</div>
}
<span class="field-validation-valid text-danger" asp-validation-for="category"></span>
</div>
<div class="checkbox">
<label>
<input asp-for="importGoods" /> Imported
</label>
</div>
<button type="submit" class="btn btn-primary">
Submit
</button>
</form>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
Вот скриншот, когда я ввожу понятие и нажимаю кнопку отправки напрямую. Как вы можете видеть, требуемая проверка на «locale» отсутствует.