2
I have the People Register, and I have the Address Register, which are in two separate classes, because the goal is that the user can have 2 or more addresses, as the delivery, and the collection. So I wanted to know how to use Partialview. in the case I managed 2 scaffold, being 1 of the register of People, and another of the address.
I wanted to know how to assemble in a single screen the person registration + the address, the research I saw is using Partialview, but I could not develop anything.
Follows 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);
}
so I created this Partialview and ran her Empty scaffold:
public PartialViewResult CadastroUsuarioEndereco()
{
return PartialView();
}
In the partial I put starting from line 1:
@{
Html.RenderPartial("Create");
}
and in the fourth line I did the same, but as it is of another controller, where I pass the address ("NomedaView","Controller")
, but hence it gave error on a lot of thing, that not of the error if rotate each screen individually.