Question

I want to use EditorTemplates to create a view which displays many models forms so I can create them and relation them.

To do that I have created a House_Extended model:

public class Extended_House
{
    public Extended_House() {}

    public House House { get; set; }
    public Person Owner { get; set; }
}

In my Create.cshtml I have:

@Html.EditorFor(model => model.House)
@Html.EditorFor(model => model.Owner)

In the Extended_HouseController I want to pass the Cities to be displayed in the House dropdownlist:

public ActionResult Create()
{
    ViewBag.Id_City = new SelectList(db.City, "Id_", "Name");
    return View();
}

The House.cshtml is in the EditorTemplates file of Extended_HouseView

@model myproject.Models.House --> here's my problem. I cant get the selectList values. How can I get them?

<div class="form-group">
    @Html.LabelFor(model => model.Id_City, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("Inmueble.Id_City", String.Empty)
        @Html.ValidationMessageFor(model => model.Id_City)
    </div>
</div>

Is there any way I can use dropdown lists with editor templates like this?

Était-ce utile?

La solution

I am pretty sure that this is what you are looking for: http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx

btw:

your Extended_House class is looking for me like it should be a ViewModel, you know what it is? If not please take a look e.g. here: http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx - it's the first link that I got about using ViewModels, there may be others but this gives you a pretty good example.

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