how to work with Partialview

Asked

Viewed 868 times

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.

2 answers

3


You have to create a file (.cshtml) separate to be your Partialview and put in it the data you want to register. For example, to create the partial _CadastrarEndereco.cshtml:

@model iClips.Models.Objetos.Pessoa

<div class="form-group">
    @Html.LabelFor("Numero")
    @Html.TextAreaFor(e => e.Numero)
</div>

<div class="form-group">
    @Html.LabelFor("Bairro")
    @Html.TextAreaFor(e => e.Bairro)
</div>

In your controller, you put the partial name:

public PartialViewResult CadastroUsuarioEndereco()
{
    return PartialView("~/Views/Cadastro/_CadastrarEndereco");
}

To render the partial you put in the view where the partial will be rendered, not the partial in question

@Html.RenderPartial("~/Views/Controller/_CadastrarEndereco.cshtml")


Note: You said in the question "so I created this Partialview.." and put the controller code, a Partialview is a part of the view, IE, is HTML. This part of your controller calls partial.

1

@{
Html.RenderPartial("~/Views/Controller/View.cshtml");
}

Replacing Controller the name of the folder where your partial view was generated and View.cshtml by the name of your partial view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.