Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:40:39+00:00 2026-05-13T05:40:39+00:00

I have a controller that should add a person to the db, but in

  • 0

I have a controller that should add a person to the db, but in the form I have fields for two kinds of person and only one should be add, then when I post it verifies if it is a pf (type 1) or pj (type 2) person, then I do the validation stuff based in the kind of person it is, that’s ok, but even if I don’t add any error because the form was completely fed, I get errors from the other fields, but I didn’t say they are required in any place, why does mvc add an error like this? And what is more weird is that auto erros change based in how many fields I’ve typed, may they be related with the form I’m feeding? I can’t understand what’s happening, help!!!

My code files:

Pessoa.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarvalhoRodrigues.Domain.Repositories.Cadastro;

namespace CarvalhoRodrigues.Domain.Cadastro
{
    public class Pessoa
    {
        public Pessoa()
        {
            this.Endereco = new List<Endereco>();
        }

        public virtual long Id { get; set; }
        public enum TipoPessoa { Fisica, Juridica }
        public virtual TipoPessoa Tipo { get; set; }
        public virtual ICollection<Endereco> Endereco { get; set; }

    }
}

PessoaFisica.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarvalhoRodrigues.Domain.Cadastro
{
    public class PessoaFisica : Pessoa
    {
        public PessoaFisica()
            : base()
        {
            this.Tipo = Pessoa.TipoPessoa.Fisica;
        }
        public virtual string CPF { get; set; }
        public virtual string Nome { get; set; }
        public virtual string Sexo { get; set; }
        public virtual DateTime DataNascimento { get; set; }
        public virtual string RG { get; set; }
        public virtual string RGOrgaoExpedidor { get; set; }
        public virtual DateTime RGDataExpedicao { get; set; }
        public virtual string Pai { get; set; }
        public virtual string Mae { get; set; }
    }
}

PessoaJuridica.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarvalhoRodrigues.Domain.Cadastro
{
    public class PessoaJuridica : Pessoa
    {
        public PessoaJuridica()
            : base()
        {
            this.Tipo = Pessoa.TipoPessoa.Juridica;
            this.Representantes = new List<Pessoa>();
        }

        public virtual string CNPJ { get; set; }
        public virtual string RazaoSocial { get; set; }
        public virtual DateTime DataConstituicao { get; set; }
        public virtual string NomeFantasia { get; set; }
        public virtual ICollection<Pessoa> Representantes { get; set; }
    }
}

Inserir.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Inserir
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="../../Scripts/carvalhorodrigues-cadastro.js"></script>
<% using (Html.BeginForm())
   { %>
<h2>Inserir Cliente</h2>
<%= Html.ValidationSummary() %>
<fieldset>
    <legend>Tipo de Pessoa</legend>
    <div>
        <%= Html.RadioButton("TipoPessoa", "PF", true, new { @class = "TipoPessoa" })%> Pessoa Física
        <%= Html.RadioButton("TipoPessoa", "PJ", false, new { @class = "TipoPessoa" })%> Pessoa Jurídica
    </div>
</fieldset>

<fieldset>
    <legend>Dados de Cadastro</legend>
    <div id="PF">
        <p>
        <label for="pf.Nome">Nome:</label> <br />
        <%= Html.TextBox("pf.Nome")%> <%= Html.ValidationMessage("pf.Nome", "*")%>
        </p>

        <p>
        <label for="pf.CPF">CPF:</label> <br />
        <%= Html.TextBox("pf.CPF")%> <%= Html.ValidationMessage("pf.CPF", "*")%>
        </p>

        <p>
        <label for="pf.Sexo">Sexo:</label> <br />
        <%= Html.RadioButton("pf.Sexo", "Masculino", true) %> Masculino
        <%= Html.RadioButton("pf.Sexo", "Feminino")%> Feminino
        </p>

        <p>
        <label for="pf.DataNascimento">Data de Nascimento:</label> <br />
        <%= Html.TextBox("pf.DataNascimento")%> <%= Html.ValidationMessage("pf.DataNascimento", "*")%>
        </p>

        <p>
        <label for="pf.RG">RG:</label> <br />
        <%= Html.TextBox("pf.RG")%> <%= Html.ValidationMessage("pf.RG", "*")%>
        </p>

        <p>
        <label for="pf.RGOrgaoExpedidor">Órgão Expedidor:</label> <br />
        <%= Html.TextBox("pf.RGOrgaoExpedidor")%> <%= Html.ValidationMessage("pf.RGOrgaoExpedidor", "*")%>
        </p>

        <p>
        <label for="pf.RGDataExpedicao">Data de Expedição:</label> <br />
        <%= Html.TextBox("pf.RGDataExpedicao")%> <%= Html.ValidationMessage("pf.RGDataExpedicao", "*")%>
        </p>

        <p>
        <label for="pf.Pai">Pai:</label> <br />
        <%= Html.TextBox("pf.Pai")%> <%= Html.ValidationMessage("pf.Pai", "*")%>
        </p>

        <p>
        <label for="pf.Mae">Mãe:</label> <br />
        <%= Html.TextBox("pf.Mae")%> <%= Html.ValidationMessage("pf.Mae", "*")%>
        </p>
    </div>

    <div id="PJ">
        <p>
        <label for="pj.RazaoSocial">Razão Social:</label> <br />
        <%= Html.TextBox("pj.RazaoSocial")%> <%= Html.ValidationMessage("pj.RazaoSocial", "*")%>
        </p>

        <p>
        <label for="pj.CNPJ">CNPJ:</label> <br />
        <%= Html.TextBox("pj.CNPJ")%> <%= Html.ValidationMessage("pj.CNPJ", "*")%>
        </p>

        <p>
        <label for="pj.NomeFantasia">Nome Fantasia:</label> <br />
        <%= Html.TextBox("pj.NomeFantasia")%> <%= Html.ValidationMessage("pj.NomeFantasia", "*")%>
        </p>

        <p>
        <label for="pj.DataConstituicao">Data de Constituição:</label> <br />
        <%= Html.TextBox("pj.DataConstituicao")%> <%= Html.ValidationMessage("pj.DataConstituicao", "*")%>
        </p>
    </div>
</fieldset>
<input type="submit" value="Cadastrar" />
<% } %>

ClientesController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CarvalhoRodrigues.Domain.Cadastro;
using CarvalhoRodrigues.Domain.Repositories.Cadastro;

namespace CarvalhoRodrigues.Controllers
{
    public class ClientesController : Controller
    {
        // GET: /clientes/
        public ActionResult Index()
        {
            return View();
        }

        // GET: /clientes/inserir/
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Inserir()
        {
            return View();
        }

        // POST: /clientes/inserir/
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Inserir(string TipoPessoa, [Bind(Exclude = "Id", Prefix="pf")]PessoaFisica pf, [Bind(Exclude = "Id", Prefix="pj")]PessoaJuridica pj)
        {
            IPessoaRepository repository = new PessoaRepository();

            if (TipoPessoa == "PF")
            {
                pf.Tipo = Pessoa.TipoPessoa.Fisica;

                if (pf.Nome == "")
                    ModelState.AddModelError("pf.Nome", "Nome não informado");
                if (pf.CPF == "")
                    ModelState.AddModelError("pf.CPF", "CPF não informado");
                if (pf.DataNascimento == new DateTime())
                    ModelState.AddModelError("pf.DataNascimento", "Data de nascimento não informada");
                if (pf.RG == "")
                    ModelState.AddModelError("pf.RG", "RG não informado");
                if (pf.RGOrgaoExpedidor == "")
                    ModelState.AddModelError("pf.RGOrgaoExpedidor", "Órgão expedidor não informado");
                if (pf.RGDataExpedicao == new DateTime())
                    ModelState.AddModelError("pf.RGDataExpedicao", "Data de expedição não informada");
            }
            else if (TipoPessoa == "PJ")
            {
                if (pj.RazaoSocial == "")
                    ModelState.AddModelError("pj.RazaoSocial", "Razão social não informada");
                if (pj.CNPJ == "")
                    ModelState.AddModelError("pj.CNPJ", "CNPJ não informado");
                if (pj.DataConstituicao == new DateTime())
                    ModelState.AddModelError("pj.DataConstituicao", "Data de constituição não informada");
            }

            if (ModelState.IsValid)
            {
                if (TipoPessoa == "PF")
                    repository.Inserir(pf);
                else if (TipoPessoa == "PJ")
                    repository.Inserir(pj);
            }

            return View();
        }
}

If I don’t type any field with the “PF” radio selected I got the following errors:

  • CPF não informado
  • Nome não informado
  • A value is required.
  • Data de nascimento não informada
  • RG não informado
  • Órgão expedidor não informado
  • A value is required.
  • Data de expedição não informada
  • A value is required.

When I think it should be only:

  • CPF não informado
  • Nome não informado
  • Data de nascimento não informada
  • RG não informado
  • Órgão expedidor não informado
  • Data de expedição não informada

And if I type all the PF fields, I get only one error:

  • A value is required.

The messages doesn’t say where the error comes from. My model classes are just gets and sets, I’m using Nhibernate, they don’t have any logic and neither any required instruction in it’s fields.

Help guyz, I’m not understanding what is happening whit the ModelState thing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:40:40+00:00Added an answer on May 13, 2026 at 5:40 am

    You probably have a non-NULL field somewhere. The “A value is required” error is added by a lower layer.

    This could be the primary key if it is not defined as autonumbering (Identity spec) or some other column that is not set from your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller method that returns a jSON object and in one calling
I am running JSF 2.0 I currently have one java controller that is called
i have a view that shows the user a form and the user should
EDIT!! I should add that this site runs perfectly locally. It's only when deployed
I have a spring mvc controller that returns a pojo : @RequestMapping(value = /person,
I have many controllers that will have some similar behavior, e.g. the user should
I have a controller that contains a tableView that spawns another controller modally to
I have a controller that is called with AJAX (sends JSON data), so I
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
When you have a controller that does logic with services and DAO's that may

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.