Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

ModelState.IsValid Equal to FALSE

$
0
0

Hello,

I need a little help to understand where is a problem. 

I have 4 tables: SparePart, SpareType, MachineType, Pdm

Relationship is next: SparePart has FK from SpareType and from MachineType, Pdm has FK from SpareType

Now,when I want to insert record into Pdm I'm getting some error related to SpareType and Machine Type are required, even if they are shown in viewModel parameters, whyModelState.IsValid is false ?

Here are code from all view models.

SparePartViewModel.cs

public class SparePartViewModel
    {
        public Int64? Id { get; set; }

        public DateTime CreateDate { get; set; }

        [Required]
        [StringLength(100)]
        public string InternalCode { get; set; }

        [StringLength(4096)]
        public string Description { get; set; }

        [StringLength(255)]
        public string NameOnFolder { get; set; }

        public decimal? Enter { get; set; }

        public decimal? Exit { get; set; }

        public decimal? Thickness { get; set; }

        public string Band { get; set; }

        public string Color { get; set; }

        public bool Elastic { get; set; }

        [Required]
        public virtual MachineType MachineType { get; set; }

        [Required]
        public virtual SpareType SpareType { get; set; }
    }

SpareTypeViewModel.cs

    public class SpareTypeViewModel
    {
        public Int64 id { get; set; }

        public DateTime CreateDate { get; set; }

        [Required]
        public string Name { get; set; }
    }

MachineTypeViewModel.cs

    public class MachineTypeViewModel
    {
        public Int64 id { get; set; }

        public DateTime CreateDate { get; set; }

        [Required]
        public string Name { get; set; }
    }

PdmViewModel.cs

    public class PdmViewModel
    {
        public Int64 Id { get; set; }

        public DateTime CreateDate { get; set; }

        [Required]
        public string PdmCode { get; set; }

        [Required]
        public virtual SparePart SparePart { get; set; }
    }

Picture from debug:

Here is the code from controller method where is called post

        [HttpPost]
        public async Task<IActionResult> Post([FromBody] PdmViewModel viewModel)
        {
            string[] entities = { "MachineType", "SpareType" };

            viewModel.SparePart = _repoSparePart.SearchByName(w => w.InternalCode == viewModel.SparePart.InternalCode, entities);

            if (ModelState.IsValid)
            {
                var newPdm = Mapper.Map<Pdm>(viewModel);
                newPdm.CreateDate = DateTime.Now;


                var exist = _repoPdm.SearchByName(w => string.Equals(w.PdmCode, viewModel.PdmCode, StringComparison.CurrentCultureIgnoreCase));
                if (exist != null)
                {
                    TempData["pdm"] = "Pdm Already Exist.";
                }
                else
                {
                    _repoPdm.Insert(newPdm);
                    if (await _repoSparePart.SaveChangesAsync())
                    {
                        TempData["pdm"] = "Pdm Code successfully created.";
                        return Created($"pdm/{newPdm.PdmCode}", Mapper.Map<Pdm>(viewModel));
                    }
                }
            }
            return BadRequest("Failed to save.");
        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>