jeudi 11 juin 2015

Create view for a child-master relationship

I have an entity called Entity, (lol).

        public class Entidad
    {
        [Key]
        public int Id { get; set; }
        public string Nombre { get; set; }

        public virtual ICollection<Propiedad> Propiedades { get; set; }
}

I also have an entity called Properties

 public class Propiedad
    {
        [Key]
        public int Id { get; set; }

        public virtual Entidad Entidad { get; set; }

        public string Codigo { get; set; }
        public string Nombre { get; set; }
        public string TipoDeDatos { get; set; }
    }

My Create view with the automated scaffolding gets rendered like this http://ift.tt/1S8gHn6

However, I should be able to select the Entity from a dropdown list.

this is the automatically generated view

@model Inspinia_MVC5.Areas.GlobalAdmin.Models.Propiedad

@{
    ViewBag.Title = "Create";
    Layout = "~/Areas/GlobalAdmin/Views/Shared/_LayoutGlobalAdmin.cshtml";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2>Create</h2>
        <ol class="breadcrumb">
            <li>
                @Html.ActionLink("List", "Index")
            </li>
            <li class="active">
                <strong>Create</strong>
            </li>
        </ol>
    </div>
    <div class="col-sm-8">
        <div class="title-action">
            @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-primary"})
        </div>
    </div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>Create Propiedad</h5>
                </div>
                <div class="ibox-content">

                @using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true)

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

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

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-primary" />
                @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-white"})
            </div>
        </div>
    </div>
}

                </div>
            </div>
        </div>
    </div>
 </div>

Questions is how can I add the dropdown and make it fill the values from the entities?

Aucun commentaire:

Enregistrer un commentaire