Why does my Endereco model return null after the postback?

Asked

Viewed 32 times

0

Why did my model Addressee returns null after the postback?:

inserir a descrição da imagem aqui

Model:

public class Paciente
{
    public Paciente()
    {
        Endereco = new List<Endereco>();
    }

    [Key]
    public int PacienteID { get; set; }
    public string Nome { get; set; }
    public string Profissao { get; set; }
    public virtual ICollection<Endereco> Endereco { get; set; }
}

public class Endereco
{
    [Key]
    public int EnderecoID { get; set; }
    public int PacienteID { get; set; }
    public string Logradouro { get; set; }
    public string NOLogradouro { get; set; }
    public virtual Paciente Paciente { get; set; }
}

HTML

@model  SGMed.Dominio.Entidade.Paciente
@{
    ViewBag.Title = null;
}

    @using (Html.BeginForm("Alterar", "Paciente", FormMethod.Post))
    {
    <p>
        <input type="submit" value="Salvar Paciente" class="btn btn-primary" />
    </p>
    <div class="row">
        <div class="col-md-12">
            <fieldset>
                <legend>Dados do Paciente</legend>            
                    <div class="col-md-7">
                        @Html.LabelFor(model => model.Nome)
                        @Html.TextBoxFor(model => model.Nome, new { @class = "form-control", style = "min-width:100%;" })
                    </div>                           
                    <div class="col-md-2">
                        <label>Profissão</label>
                        <input type="text" id="Profissao" name="Profissao" value="@Model.Profissao" class="form-control" style="min-width:100%;" />
                    </div>
            </fieldset>
        </div>
        <br />
        <div class="col-md-12">
            <fieldset>
                <legend>Dados do Endereço</legend>
                    @foreach(var item in Model.Endereco) 
                    {                   
                        <div class="row">
                            <div class="col-md-6">
                                <label>Logradouro</label>                                
                                <input type="text" id="Logradouro" name="Endereco.Logradouro" value="@item.Logradouro" class="form-control" style="min-width:100%;" />
                            </div>
                            <div class="col-md-1">
                                <label>Número</label>
                                <input type="text" id="NOLogradouro" name="Endereco.NOLogradouro" value="@item.NOLogradouro" class="form-control" style="min-width:100%;" />
                            </div>
                        </div>
                    }
            </fieldset>
        </div>
    </div>

1 answer

0


The construction of the View to edit lists and collections is somewhat peculiar and has some pitfalls. This is due to the attributes name and id of the elements vs as MVC will bind with its ViewModel.

In your Model change the type of ICollection<> for IList<>, so you can access your members by an index

public virtual IList<Endereco> Endereco { get; set; }

Now in your View, instead of using foreach by a for, making the construction of the elements as follows.

<div class="col-md-12">
    <fieldset>
        <legend>Dados do Endereço</legend>
        @for (int enderecoIndex = 0; enderecoIndex < Model.Endereco.Count; enderecoIndex++)
        {
            @Html.HiddenFor(x => @Model.Endereco[enderecoIndex].EnderecoID)
            @Html.HiddenFor(x => @Model.Endereco[enderecoIndex].PacienteID)
            <div class="row">
                <div class="col-md-6">
                    <label>Logradouro</label>
                    @Html.TextBoxFor(x => Model.Endereco[enderecoIndex].Logradouro, new { @class = "form-control", style = "min-width:100%;" })                            
                </div>
                <div class="col-md-1">
                    <label>Número</label>
                    @Html.TextBoxFor(x => Model.Endereco[enderecoIndex].NOLogradouro, new { @class = "form-control", style = "min-width:100%;" })                            
                </div>
            </div>

        }
    </fieldset>
</div>

Browser other questions tagged

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