I have the People Registry, and I have the Address Book, which are in two separate classes, because the goal is that the user can have 2 or more addresses, such as delivery, and billing. So I wanted to know how to use PartialView. in the case I generated 2 scaffold, being 1 from the People registry, and another from the address.
I wanted to know how to mount the person record + the address on a single screen, through the searches I saw using PartialView, but I could not develop anything.
Follow the components:
Classes:
public class Pessoa
{
[Key]
public int PessoaID { get; set; }
[Required(ErrorMessage = "Preencha o nome")]
[DisplayName("Nome")]
[StringLength(150, MinimumLength = 2, ErrorMessage = "O nome deve ter no mínimo 2 e no máximo 150 caracteres.")]
public string Nome { get; set; }
[DisplayName("Telefone")]
public string Telefone { get; set; }
[DisplayName("Celular")]
public string Celular { get; set; }
[DisplayName("WhatsApp")]
public string Whatsapp { get; set; }
[DisplayName("Email")]
[StringLength(150, ErrorMessage = "O E-mail deve ter no máximo 150 caracteres.")]
public string Email { get; set; }
[Required(ErrorMessage = "Preencha o CPF")]
[DisplayName("CPF")]
[StringLength(14, MinimumLength = 14, ErrorMessage = "O CPF deve 14 caracteres.")]
public string CPF { get; set; }
[DisplayName("CNPJ")]
public string CNPJ { get; set; }
public int Tipo { get; set; }
//relacionamentos
public virtual ICollection<PessoaEndereco> PessoasEnderecos { get; set; }
}
public class Endereco
{
[Key]
public int EnderecoID { get; set; }
[Required(ErrorMessage = "Preencha o endereço")]
[DisplayName("Endereço")]
[StringLength(150, MinimumLength = 2, ErrorMessage = "O endereço deve ter no mínimo 2 e no máximo 150 caracteres.")]
public string Descricao { get; set; }
[Required(ErrorMessage = "Preencha o numero")]
[DisplayName("Numero")]
//[StringLength(5, MinimumLength = 1, ErrorMessage = "O numero da residência deve ter no mínimo 1 e no máximo 5 caracteres.")]
public int Numero { get; set; }
[Required(ErrorMessage = "Preencha o bairro")]
[DisplayName("Bairro")]
[StringLength(100, MinimumLength = 3, ErrorMessage = "O bairro deve ter no mínimo 3 e no máximo 100 caracteres.")]
public string Bairro { get; set; }
[Required(ErrorMessage = "Preencha o CEP")]
[DisplayName("CEP")]
[StringLength(9, MinimumLength = 9, ErrorMessage = "O CEP possui 9 caracteres.")]
public string CEP { get; set; }
[DisplayName("Comlemento")]
public string Complemento { get; set; }
//Relacionamentos
public int LogradouroID { get; set; }
public virtual Logradouro Logradouro { get; set; }
public int CidadeID { get; set; }
public virtual Cidade Cidade { get; set; }
public virtual ICollection<PessoaEndereco> PessoasEnderecos { get; set; }
}
The Controller:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PessoaID,Nome,Telefone,Celular,Whatsapp,Email,CPF,Tipo,RG,Senha")] Pessoa pessoa)
{
pessoa.Tipo = 1;
if (ModelState.IsValid)
{
db.Pessoas.Add(pessoa);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pessoa);
}
Then I created this PartialView and generated the empty scaffold:
public PartialViewResult CadastroUsuarioEndereco()
{
return PartialView();
}
In partial put starting line 1:
@{
Html.RenderPartial("Create");
}
and in the fourth line I did the same, but as it is from another controller, where I pass the address I put ("NomedaView","Controller")
, but then he gave a lot of error, not the error if I run each screen individually. p>