I'm having problem trying to use a model which is defined in a dll. I have imported the namespace to the model in _imports.razor. But still when I'm trying to use it on a page i get the error 'The name 'UserRegisterViewModel' does not exist in the current context'. Does anyone know what the problem might be? Thanks!
Edit:
I get the error in the htlm when trying to use UserRegisterViewModel, for example <EditForm Model="UserRegisterViewModel" OnValidSubmit="HandleRegistration">
Model
public class UserRegisterViewModel
{
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
Page
#page "/register"
#inject IAuthService AuthService
#inject NavigationManager NavigationManager
<h1>Register</h1>
#if (ShowErrors)
{
<div class="alert alert-danger" role="alert">
#foreach (var error in Errors)
{
<p>#error</p>
}
</div>
}
<div class="card">
<div class="card-body">
<h5 class="card-title">Please enter your details</h5>
<EditForm Model="UserRegisterViewModel" OnValidSubmit="HandleRegistration">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="email">Email address</label>
<InputText Id="email" class="form-control" #bind-Value="UserRegisterViewModel.Email" />
<ValidationMessage For="#(() => UserRegisterViewModel.Email)" />
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText Id="password" type="password" class="form-control" #bind-Value="UserRegisterViewModel.Password" />
<ValidationMessage For="#(() => UserRegisterViewModel.Password)" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</div>
</div>
#code {
private UserRegisterViewModel RegisterModel = new UserRegisterViewModel();
private bool ShowErrors;
private IEnumerable<string> Errors;
private async Task HandleRegistration()
{
ShowErrors = false;
var result = await AuthService.Register(UserRegisterViewModel);
if (result.Successful)
{
NavigationManager.NavigateTo("/login");
}
else
{
Errors = result.Errors;
ShowErrors = true;
}
}
}
Thanks!
Model="UserRegisterViewModel" your binding to the type not the instance RegisterModel
#page "/register"
#inject IAuthService AuthService
#inject NavigationManager NavigationManager
<h1>Register</h1>
#if (ShowErrors)
{
<div class="alert alert-danger" role="alert">
#foreach (var error in Errors)
{
<p>#error</p>
}
</div>
}
<div class="card">
<div class="card-body">
<h5 class="card-title">Please enter your details</h5>
<EditForm Model="RegisterModel" OnValidSubmit="HandleRegistration">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="email">Email address</label>
<InputText Id="email" class="form-control" #bind-Value="RegisterModel.Email" />
<ValidationMessage For="#(() => RegisterModel.Email)" />
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText Id="password" type="password" class="form-control" #bind-Value="RegisterModel.Password" />
<ValidationMessage For="#(() => RegisterModel.Password)" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</div>
</div>
#code {
private UserRegisterViewModel RegisterModel = new UserRegisterViewModel();
private bool ShowErrors;
private IEnumerable<string> Errors;
private async Task HandleRegistration()
{
ShowErrors = false;
var result = await AuthService.Register(RegisterModel);
if (result.Successful)
{
NavigationManager.NavigateTo("/login");
}
else
{
Errors = result.Errors;
ShowErrors = true;
}
}
}
Related
my html code
<div class="border backgroundWhite border-info">
<div class="row">
<div class="col-6">
<div class="form-group row">
<div class="col-4">
<label asp-for="Shift.TSBalance"></label>
</div>
<div class="col-6">
<input asp-for="Shift.TSBalance" class="form-control" id="sbalance"/>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="Shift.TSupply"></label>
</div>
<div class="col-6">
<input asp-for="Shift.TSupply" class="form-control" id="supply" value="0" onkeyup="sum()" type="text"/>
</div>
<span asp-validation-for="Shift.TSupply" class="text-danger"></span>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="Shift.TTotal"></label>
</div>
<div class="col-6">
<input asp-for="Shift.TTotal" class="form-control" readonly id="trbalance" value="0" onkeyup="sum()" type="text"/>
</div>
<span asp-validation-for="Shift.TTotal" class="text-danger"></span>
</div>
</div>
<div class="col-6">
<div class="form-group row">
<div class="col-7">
<label asp-for="Shift.TCBalance"></label>
</div>
<div class="col-5">
<input asp-for="Shift.TCBalance" class="form-control" id="calcebalance" value="0" onkeyup="sum()" type="text" readonly/>
</div>
</div>
<div class="form-group row">
<div class="col-7">
<label asp-for="Shift.TABalance"></label>
</div>
<div class="col-5">
<input asp-for="Shift.TABalance" class="form-control" id="actualebalance" value="0" onkeyup="sum()" type="text" />
</div>
</div>
<div class="form-group row">
<div class="col-7">
<label asp-for="Shift.TDifferance" class="text-info"></label>
</div>
<div class="col-5">
<input asp-for="Shift.TDifferance" class="form-control" style="background-color:blue;color:white;font-size:larger" id="differance" value="0" onkeyup="sum()" type="text" readonly />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
my page model
namespace Elwady.Pages.ShiftClosing
{
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _db;
public SelectList Exlist { get; set; }
[BindProperty]
public Shift Shift { get; set; }
public CreateModel(ApplicationDbContext db)
{
_db = db;
}
public IActionResult OnGet()
{
this.Exlist = new SelectList(_db.ExpensesList, "Id", "ExName");
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
};
_db.Shift.Add(Shift);
await _db.SaveChangesAsync();
return RedirectToPage("../Index");
}
}
}
my data table
public class Shift
{
public int Id { get; set; }
public int TSBalance { get; set; }
public int TSupply { get; set; }
public int TTotal { get; set; }
public int TCBalance { get; set; }
public int TABalance { get; set; }
public int TDifferance { get; set; }
}
attached image to show clearly how you can support
i tried to get this value through assigning it to variable in get handler but i can't and tried to Asp-for model variable but i get an error in the Ui
if i need to print out this form after submit to specific printer, how can i do that?
I'm facing an odd issue here.
I have a page where i have setup a PageRemote attribute to validate a field, and on the same page, i have 2 buttons that each call a distinct handler using the asp-page-handler property (1 to update the content, the other to delete it).
If i remove the PageRemote validation, the handlers for both buttons are triggered as expected, but when i leave the PageRemote validation on the page, the buttons specific handlers are not called, although the PageRemote handler is correctly triggered.
Furthermore, when leaving the PageRemote validation on the page, if i force its validation to return false, then afterwards the buttons trigger their respective handler, but if i do not force it, then the behaviour is as described earlier, where the handlers are not triggered.
Can someone explain me if there is a catch on using these two things on the same page, and/or how to overcome this?
Below is a sample of the page code:
public class EditModel : PageModel
{
private IConfiguration _configuration;
[Required(ErrorMessageResourceType = typeof(ErrorMessages),
ErrorMessageResourceName = nameof(ErrorMessages.SlugRequired))]
[MinLength(3, ErrorMessageResourceType = typeof(ErrorMessages),
ErrorMessageResourceName = nameof(ErrorMessages.SlugMinLength))]
[MaxLength(128, ErrorMessageResourceType = typeof(ErrorMessages),
ErrorMessageResourceName = nameof(ErrorMessages.SlugMaxLength))]
[RegularExpression(#"^[0-9a-zA-Z_/-]*$", ErrorMessageResourceType = typeof(ErrorMessages),
ErrorMessageResourceName = nameof(ErrorMessages.SlugAllowedCharacters))]
[PageRemote(ErrorMessageResourceType = typeof(ErrorMessages),
ErrorMessageResourceName = nameof(ErrorMessages.SlugDuplicate),
AdditionalFields = "__RequestVerificationToken,GenericContentDTO.GenericContent.GenericContentId",
HttpMethod = "post",
PageHandler = "CheckSlug")]
[Display(Name = "URL Title")]
[BindProperty]
public string Slug { get; set; }
[TempData]
public string FormResultMessage { get; set; }
[BindProperty]
public GenericContentDTO GenericContentDTO { get; set; }
public EditModel(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult OnGet(Guid genericContentId)
{
if (genericContentId.ToString() == "")
{
return NotFound();
}
...
Code to load content
...
return Page();
}
public async Task<IActionResult> OnPostUpdate()
{
if (!ModelState.IsValid)
{
return Page();
}
...
Code to update content
...
return RedirectToPage(new { GenericContentId = GenericContentDTO.GenericContent.GenericContentId });
}
public IActionResult OnPostDelete()
{
...
Code to delete content
}
public JsonResult OnPostCheckSlug()
{
var token = HttpContext.Session.GetString("APIAuthorizationToken");
CheckSlugDTO CheckSlug = new CheckSlugDTO
{
Slug = Slug,
ContentId = GenericContentDTO.GenericContent.GenericContentId,
Exists = true
};
CheckSlug = APICommunicationHelper.PostData<CheckSlugDTO>(CheckSlug, _configuration["AppConfigs:APIAddress"] + "api/CheckGenericContentSlugExistance", token);
return new JsonResult(!CheckSlug.Exists);
}
}
And of the Razor Code:
#page "{GenericContentId:Guid}"
#model MyProject.Pages.Generic_Contents.EditModel
#{
}
<form method="post" id="editForm" name="editForm" enctype="multipart/form-data">
<h3>#TempData["FormResultMessage"]</h3>
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" data-toggle="tab" href="#main">Main</a></li>
<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#meta">Meta</a></li>
</ul>
<div class="tab-content">
<div id="main" class="tab-pane active">
<br />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
#Html.HiddenFor(model => model.GenericContentDTO.GenericContent.GenericContentId)
<div class="form-group">
<label asp-for="GenericContentDTO.GenericContent.Title" class="control-label"></label>
<input asp-for="GenericContentDTO.GenericContent.Title" class="form-control" />
<span asp-validation-for="GenericContentDTO.GenericContent.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Slug" class="control-label"></label>
<input asp-for="Slug" class="form-control" />
<span asp-validation-for="Slug" class="text-danger"></span>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="GenericContentDTO.MainImage" class="control-label"></label>
<input asp-for="GenericContentDTO.MainImage" type="file" class="form-control" />
</div>
</div>
<div class="form-group">
<label asp-for="GenericContentDTO.GenericContent.Summary" class="control-label"></label>
#Html.TextAreaFor(model => model.GenericContentDTO.GenericContent.Summary, new { #class = "form-control", #rows = 5 })
<span asp-validation-for="GenericContentDTO.GenericContent.Summary" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="GenericContentDTO.GenericContent.ContentText" class="control-label"></label>
#Html.TextAreaFor(model => model.GenericContentDTO.GenericContent.ContentText, new { #class = "form-control editorHtml" })
</div>
<div class="form-group">
<label asp-for="GenericContentDTO.GenericContent.IsActive" class="control-label"></label>
#Html.CheckBoxFor(model => model.GenericContentDTO.GenericContent.IsActive)
</div>
</div>
<div id="meta" class="tab-pane fade">
<div class="form-group">
<label asp-for="GenericContentDTO.GenericContent.MetaDescription" class="control-label"></label>
#Html.TextAreaFor(model => model.GenericContentDTO.GenericContent.MetaDescription, new { #class = "form-control", #rows = 5 })
<span asp-validation-for="GenericContentDTO.GenericContent.MetaDescription" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="GenericContentDTO.GenericContent.MetaKeywords" class="control-label"></label>
#Html.TextAreaFor(model => model.GenericContentDTO.GenericContent.MetaKeywords, new { #class = "form-control", #rows = 5 })
<span asp-validation-for="GenericContentDTO.GenericContent.MetaKeywords" class="text-danger"></span>
</div>
</div>
<div class="form-group text-right">
<a asp-area="" asp-page="/Generic-Contents" class="btn btn-secondary">Cancel</a>
<button type="submit" class="btn btn-danger" asp-page-handler="Delete">Delete Content</button>
<button type="submit" class="btn btn-primary" asp-page-handler="Update">Update Content</button>
</div>
</div>
</form>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Thanks
trying to submit the commenting form,but the form send null data to my razor page.
it is the html:
<div class="comment-form-container">
<h6>Leave a Comment</h6>
<form method="post">
<div class="input-prepend">
<span class="add-on"><i class="icon-user"></i></span>
<input name="Name" class="span4" id="Name" size="16" type="text" placeholder="Name">
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input name="Email" class="span4" id="Email" size="16" type="email" placeholder="Email Address">
</div>
<input type="hidden" name="ArticleId" id="ArticleId" value="#Model.articleQueryView.Id" />
<textarea class="span6" name="Message" id="Message"></textarea>
<div class="row">
<div class="span2">
<input type="submit" class="btn btn-inverse" value="Post My Comment">
</div>
</div>
</form>
</div>
and here is the CsHtml code,when in hover the "comment" i just see the null and 0 value:
public class blog_singleModel : PageModel
{
public ArticleQueryView articleQueryView { get; set; }
private readonly ICommentApplication _commentApplication;
private readonly IArticleQuery _query;
public blog_singleModel(IArticleQuery query, ICommentApplication commentApplication)
{
_query = query;
_commentApplication = commentApplication;
}
public void OnGet(int id)
{
articleQueryView = _query.GetArticle(id);
}
public RedirectToPageResult OnPost(AddNewComment comment)
{
_commentApplication.Add(comment);
return RedirectToPage("./blog_single", new { id = comment.ArticleId });
}
}
}
comment.Id is 0 is because your articleQueryView.Id is 0.Name,Email,Message is null is because you did not set the values of them in form.I think your code is correct,but you did not set the values.
sample:
I use Modal and Load Partial view to it, but if I keep fields blank no validation happened and modal closed after click submit button.
Also I use Required annotation in my viewmodel. Also when I click submit it calls action direct in controller.
My Question: how can I display a validation message in modal form?
#section scripts{
<script>
function onFailureDefault(data) {
$("#AddOrderDetailModalDiv").valid()
}
function OpenAddOrderDetailTempItemOnSuccess() {
$("#AddOrderDetailModalDiv").modal("show");
}
function AddOrderDetailTempItemOnComplete() {
$("#AddOrderDetailModalDiv").modal("hide");
}
</script>
}
#model OrderDetailViewModel
#using (Html.BeginForm("AddOrderDetailTempItem", "Orders", FormMethod.Post, new
{
id = "AddForm",
#data_ajax = "true",
#data_ajax_method = "post",
#data_ajax_update = "#OrderDetailList",
#data_ajax_failure = "onFailureDefault",
#data_ajax_complete = "AddOrderDetailTempItemOnComplete"
}))
{
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg" role="document">
<div class="modal-content">
<div class="modal-header bg-warning white">
<h5 class="modal-title" id="exampleModalCenterTitle">#( Model.ID > 0 ? "Edit Product" : "Add Product" )</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div>
#Html.HiddenFor(m => m.ID)
<div asp-validation-summary="All" class="text-danger"></div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label asp-for="ProductId" class="control-label"></label>
#Html.DropDownListFor(m => m.ProductId, Model.ProductsList, " ", htmlAttributes: new { #class = "select2 form-control" })
<span asp-validation-for="ProductId" class="text-danger"></span>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label asp-for="Quantity" class="control-label"></label>
<input asp-for="Quantity" required class="form-control" />
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label asp-for="Unit" class="control-label"></label>
#Html.DropDownListFor(m => m.Unit, Model.QuantityUnitsList, " ", htmlAttributes: new { #class = "form-control" })
<span asp-validation-for="Unit" class="text-danger"></span>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label asp-for="UnitPrice" class="control-label"></label>
<input asp-for="UnitPrice" class="form-control" />
</div>
</div>
<label id="ErrorMessage" asp-for="ErrorMessage" class="form-control" />
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Add Product" id="postSave" class="btn btn-primary" />
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
}
[HttpPost]
public ActionResult AddOrderDetailTempItem(OrderDetailViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
List<OrderDetailViewModel> models = OrderDetailViewModelList;
var product = productService.SingleBy(model.ProductId);
if (model.ID > 0)
{
var updatedItem = models.Find(m => m.ID == model.ID);
updatedItem.Quantity = model.Quantity;
updatedItem.ProductId = model.ProductId;
updatedItem.UnitPrice = model.UnitPrice;
updatedItem.TotalPrice = model.Quantity * model.UnitPrice;
updatedItem.ProductName = product.Name;
updatedItem.Unit = model.Unit;
updatedItem.QuantityUnitsText = ((QuantityUnits)model.Unit).ToString();
updatedItem.isActive = true;
//mapper.Map(model,updatedItem);///
}
else
{
models.Add(new OrderDetailViewModel
{
ID = counter - 1,//models.Count + 1,
Quantity = model.Quantity,
ProductId = model.ProductId,
UnitPrice = model.UnitPrice,
TotalPrice = model.Quantity * model.UnitPrice,
ProductName = product.Name,
Unit = model.Unit,
QuantityUnitsText = ((QuantityUnits)model.Unit).ToString(),
isActive = true,
});
counter -= 1;
}
OrderDetailViewModelList = models;
return PartialView("_OrderDetailGridPartial", OrderDetailViewModelList.Where(a => a.isActive == true).ToList());
}
Parent View
#using (Html.BeginForm("AddOrder", "Orders", FormMethod.Post, new
{
id = "AddForm",
#data_ajax = "true",
#data_ajax_method = "post",
//#data_ajax_update = "#OrderDetailList",
#data_ajax_complete = "AddOrderOnComplete"
}))
{
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label asp-for="DeliveryDate" class="control-label"></label>
<input asp-for="DeliveryDate" class="form-control" type="date" />
<span asp-validation-for="DeliveryDate" class="text-danger"></span>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label asp-for="OrderDate" class="control-label"></label>
<input asp-for="OrderDate" class="form-control" disabled type="date" />
<span asp-validation-for="OrderDate" class="text-danger"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label asp-for="CustomerId" class="control-label"></label>
#Html.DropDownListFor(m => m.CustomerId, Model.CustomersList, "Please select", htmlAttributes: new { #class = "select2 form-control" })
<span asp-validation-for="CustomerId" class="text-danger"></span>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label asp-for="Notes" class="control-label"></label>
<textarea asp-for="Notes" rows="4" class="form-control"></textarea>
<span asp-validation-for="Notes" class="text-danger"></span>
</div>
</div>
</div>
<div class="content-header row">
<div class="content-header-left col-md-9 col-12 mb-2">
</div>
<div class="content-header-right text-md-right col-md-3 col-12">
<div class="form-group breadcrum-right">
<button type="button" class="btn bg-gradient-danger mr-1 mb-1 waves-effect waves-light">
<i class="feather icon-package">
#Html.ActionLink("Add Product", "AddOrderDetailTempItem", "Orders", routeValues: null, htmlAttributes: new
{
Style = "color:White;font-family:'Montserrat';padding-left: 10px;",
#data_ajax = "true",
#data_ajax_method = "Get",
#data_ajax_update = "#AddOrderDetailModalDiv",
#data_ajax_failure = "onFailureDefault",
#data_ajax_success = "OpenAddOrderDetailTempItemOnSuccess",
})
</i>
</button>
</div>
</div>
</div>
<div id="OrderDetailList">
<partial name="_OrderDetailGridPartial.cshtml" model="Model.OrderDetailViewModel" />
</div>
<div class="form-group col-sm-3">
<input type="submit" name="btn" value="Save" class="btn btn-primary" />
</div>
}
My Model
public class OrderDetailViewModel
{
public int ID { get; set; }
public int OrderId { get; set; }
[Required(ErrorMessage = "حقل إجباري")]
[Display(Name ="Product Name")]
public int ProductId { get; set; }
public string ProductName { get; set; }
[Required(ErrorMessage = "حقل إجباري")]
public int Unit { get; set; }
[Required(ErrorMessage = "حقل إجباري")]
public int Quantity { get; set; }
[Required(ErrorMessage = "حقل إجباري")]
public decimal UnitPrice { get; set; }
public decimal TotalPrice { get; set; }
public SelectList ProductsList { get; set; }
public SelectList QuantityUnitsList { get; set; }
public bool isActive { get; set; }
public string QuantityUnitsText { get; set; }
public string ErrorMessage { get; set; }
}
Your code is not complete,so I provide a work demo,you can check it.
Model:
public class Student
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
_OrderDetailGridPartial:
#model Student
<div id="MyModal" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header" >
<h4 class="modal-title" >Update Role</h4>
</div>
<div class="modal-body">
<form id="myform" method="post">
<div>
<label>Id</label>
<input asp-for="Id" />
<span asp-validation-for="Id" class="text-danger"></span>
</div>
<div>
<label>Name</label>
<input asp-for="Name" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<button id="button" class="btn btn-default">Save</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Index View:
<button id="showmodal" class="btn btn-danger">
Click
</button>
<div id="partical">
<partial name="~/Views/Shared/_OrderDetailGridPartial.cshtml" />
</div>
#section scripts{
<script>
$("#showmodal").click(function () {
$('#MyModal').modal('show');
})
$("#button").click(function (e) {
e.preventDefault();
var model = $('#myform').serialize();
console.log(model);
$.ajax({
type: 'POST',
url: 'home/index',
async: false,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data: model,
success: function (result) {
$('#partical').html(result);
$('#MyModal').modal('show');
}
});
})
</script>
}
Action:
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(Student student)
{
if (!ModelState.IsValid)
{
return PartialView("_OrderDetailGridPartial",student);
}
return View();
}
Test result
<span asp-validation-for="Password" class="text-danger"></span>
<div class="form-group">
<label asp-for="Password">პაროლი</label>
#Html.TextBoxFor(x => x.Password, new { type = "password",#class = "form-control" })
<!--
<input asp-for="Password" class="form-control" />
-->
</div>
This is Sample input. I am doing Server-side validation only. How can I make input field border red when user input is not validated?
Model Validation feature adds the CSS Class input-validation-error to all the controls that have failed the validation checks .
Here is a example you could refer to :
1.Model
public class User
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[StringLength(8, ErrorMessage = "{0} length must be between {2} and {1}.", MinimumLength = 6)]
public string Password { get; set; }
}
2.View
<style type="text/css">
.input-validation-error {
border-color: yellowgreen;
}
</style>
<div class="row">
<div class="col-md-4">
<form asp-action="Create" class="myForm">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password">პაროლი</label>
<input asp-for="Password" type="password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#section Scripts
{
#{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
Result :
Reference : https://www.yogihosting.com/aspnet-core-model-validation/