locked
how can i bring the error message given in controller into the view span asp-validation please help RRS feed

  • Question

  • User-1355965324 posted

    Here is my model

     public class Vehicles
        {
            [Key]
            public int Id { get; set; }
            
            public string Make { get; set; }
        }
        

    view file

     <div class="col-md-6">
                <div class="form-group row">
                    <label asp-for="Vehicles.Make" class="control-label  col-3 col-form-label">Make</label>
                    <div class="col-9">
                        <input class="form-control input-sm" type="text" id="TxtMake"
                               asp-for="Vehicles.Make" />
                        <span asp-validation-for="Vehicles.Make" class="text-danger">@ViewBag.MakeValError</span>
                    </div>
                </div>
            </div>
    <button type="submit" class="btn btn-primary form-control">Create</button>

    controller

    [HttpPost]
    public async Task<IActionResult> UpsertVehicle(VehicleVM vehicleVM)
            {
              
                if (vehicleVM.Vehicles.Make.ToString() == "")
                {
                    ViewBag.MakeValError = "Please give vehicle make";  // If the value is null  , the message should be given in span validate control
    
                }
                else
    		{
     		_unitOfWork.VehicleRepo.Add(vehicleVM.Vehicles);
    		}
    
    }

    Tuesday, June 16, 2020 11:10 AM

Answers

  • User475983607 posted

    Required clause  cannot be given in model. 

    That's not true.  Most likely the standard [Required] attribute does not work in your design for some unknown reason?!

    So I am trying to give it from controller

    I don't understand the approach or why you are not using standard MVC features.  I built a working demo using your code.  although, I had to fix several minor issues first.

            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(VehicleVM vehicleVM)
            {
                if (String.IsNullOrEmpty(vehicleVM.Vehicles.Make))
                {
                    ModelState.AddModelError("Vehicles.Make", "Please give vehicle make");
                    ViewBag.MakeValError = "Please give vehicle make";
                    return View(vehicleVM);
                }
    
                return View();           
            }
    @model MvcBasic.Models.VehicleVM
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <h4>BasicVm</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Index">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="col-md-6">
                    <div class="form-group row">
                        <label asp-for="Vehicles.Make" class="control-label col-3 col-form-label">Make</label>
                        <div class="col-9">
                            <input class="form-control input-sm" type="text" asp-for="Vehicles.Make" />
                            <span asp-validation-for="Vehicles.Make" class="text-danger">@ViewBag.MakeValError</span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    

    Also, I need to make a few recommendations regarding forum etiquette.  Share all the relevant code.  If the relevant code is too large  then create a demo, like I did above, that demonstrates the problem. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 16, 2020 12:17 PM

All replies

  • User475983607 posted

    I recommend learning how to use standard data annotation rather than building a custom solution.

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation?view=aspnetcore-3.1

    public class Vehicles
    {
        [Key]
        public int Id { get; set; }
        
        [Required]
        public string Make { get; set; }
    }

    Tuesday, June 16, 2020 11:25 AM
  • User-1355965324 posted

    Required clause  cannot be given in model.  So I am trying to give it from controller

    Tuesday, June 16, 2020 11:41 AM
  • User475983607 posted

    Required clause  cannot be given in model. 

    That's not true.  Most likely the standard [Required] attribute does not work in your design for some unknown reason?!

    So I am trying to give it from controller

    I don't understand the approach or why you are not using standard MVC features.  I built a working demo using your code.  although, I had to fix several minor issues first.

            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(VehicleVM vehicleVM)
            {
                if (String.IsNullOrEmpty(vehicleVM.Vehicles.Make))
                {
                    ModelState.AddModelError("Vehicles.Make", "Please give vehicle make");
                    ViewBag.MakeValError = "Please give vehicle make";
                    return View(vehicleVM);
                }
    
                return View();           
            }
    @model MvcBasic.Models.VehicleVM
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <h4>BasicVm</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Index">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="col-md-6">
                    <div class="form-group row">
                        <label asp-for="Vehicles.Make" class="control-label col-3 col-form-label">Make</label>
                        <div class="col-9">
                            <input class="form-control input-sm" type="text" asp-for="Vehicles.Make" />
                            <span asp-validation-for="Vehicles.Make" class="text-danger">@ViewBag.MakeValError</span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    

    Also, I need to make a few recommendations regarding forum etiquette.  Share all the relevant code.  If the relevant code is too large  then create a demo, like I did above, that demonstrates the problem. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 16, 2020 12:17 PM
  • User-1355965324 posted

    Many Thanks mgebhred. Actually that is not for Vehicle Make column. It is for Tax date column. in some screen the tax date should be passed as null in post method but in another scrfeen  the tax date should not be allowed  without value.  ModelState.AddModelError is working for that.  I did the same way you advised me . It is working fine . /many thanks

    Tuesday, June 16, 2020 12:26 PM