Question

I tried to render model in View with Html.EditorForModel. This is code of my Model.
Product class

public class Product
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int ProductId { get; set; }

    public string Name { get; set; }
    public string Model { get; set; }
    public string Serial { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int DealerId { get; set; }

    [ForeignKey("DealerId")]
    public virtual Dealer Dealer { get; set; }
}

Dealer class

public class Dealer
{
    [Key]
    public int DealerId { get; set; }

    public string Name { get; set; }
    public int DealerNumber { get; set; }
}

I try do render Product for editing. I do it this way

@using (Html.BeginForm("Edit","Product"))
{        
    @Html.EditorForModel()
    @Html.EditorForModel(Model.Dealer)
    <input class="btn btn-default" type="submit" value="edit" />
}

But it seems like View render EditorForModel() twice. This is screen View Edit

Is it way to resolve this problem?

Était-ce utile?

La solution

Indeed you're calling @Html.EditorForModel twice. Try this instead :

@using (Html.BeginForm("Edit","Product"))
{        
    @Html.EditorForModel()
    @Html.EditorFor(model => model.Dealer)
    <input class="btn btn-default" type="submit" value="edit" />
}

Autres conseils

Instead Using @Html.EditorForModel() twice use it single time..

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top