alinaish
alinaish

Reputation: 486

Rendering Model inside Model ASP.Net MVC

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?

Upvotes: 0

Views: 1582

Answers (2)

Praveen S
Praveen S

Reputation: 650

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

Upvotes: 0

R&#233;da Mattar
R&#233;da Mattar

Reputation: 4381

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" />
}

Upvotes: 4

Related Questions