locked
Not mapped item of the model is not being taken in Azure database RRS feed

  • Question

  • User-1355965324 posted

    I am using the following model  to create the vehicle record 

     public class Vehicles
        {
            [Key]
            public int Id { get; set; }
            [Required]
            public string Make { get; set; }
            [Required]
            public string Model { get; set; }
            [Required]
            public string RegNo { get; set; }
            [Required]
            public string VehicleName {get;set;}
            public DateTime? MOTDate { get; set; }
    
            [NotMapped]
            public string MOTDateString
            {
                get
                {
                    return MOTDate?.ToString("dd/MM/yyyy");
                }
            }
    
            public DateTime? TaxDate { get; set; }
    
            [NotMapped]
            public string TaxDateString
            {
                get
                {
                    return TaxDate?.ToString("dd/MM/yyyy");
                }
            }
            public DateTime? RegDate { get; set; }
    
            [NotMapped]
            public string RegDateString
            {
                get
                {
                    return RegDate?.ToString("dd/MM/yyyy");
                }
            }
            public DateTime? InsuredDate { get; set; }
            [NotMapped]
            public string InsuredDateString
            {
                get
                {
                    return InsuredDate?.ToString("dd/MM/yyyy");
                     
                }
            }
    
            public string Comment { get; set; }
            [Required]
            public bool IsDeleted { get; set; } = false;
            [Required]
            public int VehicleStatusId { get; set; }
    
            [ForeignKey("VehicleStatusId")]
            public VehicleStatus VehicleStatus { get; set; }
    
            public int VehicleTypeId { get; set; }
            [ForeignKey("VehicleTypeId")]
            public VehicleType VehicleType { get; set; }     
           
        }

    In my controller

     [HttpPost]
            public async Task<IActionResult> UpsertVehicle(VehicleVM vehicleVM)
            {
                
               //string lsdate = vehicleVM.Vehicles.TaxDate.ToString("dd MMMM yyyy hh:mm:ss tt");
                if (vehicleVM.Vehicles.MOTDate.ToString() == "")
                {
                    ViewBag.moterror = "MOTDate date must be given";
                    ModelState.AddModelError("MOTDate", ViewBag.moterror);             
                }
                if (vehicleVM.Vehicles.TaxDate.ToString() == "")
                {
                    ViewBag.taxerror = "Tax date date must be given";
                    ModelState.AddModelError("TaxDate", ViewBag.taxerror);
                }
                if (vehicleVM.Vehicles.InsuredDate.ToString() == "")
                {
                    ViewBag.InsError = "Insurance  date must be given";
                    ModelState.AddModelError("InsuredDate", ViewBag.InsError);
                }
                if (vehicleVM.Vehicles.RegDate.ToString() == "")
                {
                    ViewBag.regError = "RegDate date must be given";
                    ModelState.AddModelError("RegDate", ViewBag.regError);
                }

    While running the application in Azure,  it always shows   ModelState.AddModelError always showing  for  MOT , TAX, INSURANCE , regdate   date column, even if I have given the value . But  it is working local application. NotMapped column is  always coming as null  and shows error  while running the application in Azure

    Tuesday, June 23, 2020 12:07 PM

All replies

  • User475983607 posted

    The most common reason for null parameters is the inputs names do not match the parameters.  You did not share any of the relevant code; View Model or Markup. 

    I recommend using the dev tools view the request parameter and make sure the parameters match the View Model property names 

      always coming as null  and shows error  while running the application in Azure

    What is the error?

    Tuesday, June 23, 2020 12:27 PM
  • User-1355965324 posted

    mgebhred

    I think the javascript datepicker  has some problem with Azure site.  The  datepicker format 'dd/mm/yyyy'  is not working   when I run in azure  site https://vehiclexxxxx.azurewebsites.net

     <div class="row">
            <div class="col-md-6">
                <div class="form-group row">
                    <label asp-for="Vehicles.InsuredDate" class="control-label col-3 col-form-label">Date insured</label>
                    <div class="col-9">
                        <input class="form-control input-sm" type="text" id="InsuredDate"
                               asp-for="Vehicles.InsuredDate" />
                        <span asp-validation-for="Vehicles.InsuredDate" class="text-danger">@ViewBag.InsError</span>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group row">
                    <label asp-for="Vehicles.RegDate" class="control-label col-3 col-form-label">Reg Date</label>
                    <div class="col-9">
                        <input class="form-control input-sm" type="text" id="RegDate"
                               asp-for="Vehicles.RegDate" />
                        <span asp-validation-for="Vehicles.RegDate" class="text-danger">@ViewBag.regError</span>
                    </div>
                </div>
    
            </div>
        </div>
    
    
      <script>
            $("#TaxDate").datepicker({ dateFormat: "dd/mm/yy" });
            $("#MOTDate").datepicker({ dateFormat: "dd/mm/yy" });
            $("#InsuredDate").datepicker({ dateFormat: "dd/mm/yy" });
            $("#RegDate").datepicker({ dateFormat: "dd/mm/yy" });

    Tuesday, June 23, 2020 12:44 PM
  • User475983607 posted

    I think the javascript datepicker  has some problem with Azure site.  The  datepicker format 'dd/mm/yyyy'  is not working   when I run in azure  site <a href="https://vehiclexxxxx.azurewebsites.net
    ">https://vehiclexxxxx.azurewebsites.net

    If you think the date format or the date picker is causing a problem then take the next step and isolate the format or the dateoicker.  Create a simple test to figure out what's happening.  

    For example, create a simple model with just a date, a Get and POST Action, and a View with a date input.  Enter a date and submit the form.  Verify the the model binding functioned as expected in the POST Action.  Do this until you get the test working.  Then add the date picker and repeat the test. 

    IMHO, you really need to learn how to troubleshoot code.

    Tuesday, June 23, 2020 1:15 PM