mardi 16 juin 2015

dropdown value not saved in asp.net mvc

I have the following entities:

 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; }
        }

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

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

and in my create view I have this:

<div class="form-group">
        @Html.LabelFor(model => model.Entidad, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.Entidad.Id, (SelectList)(ViewBag.EntidadList), "Seleccionar", new { @class = "form-control" })
        </div>
    </div>

When I debug I can see this on the controller action

 // POST: /GlobalAdmin/Propiedades/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://ift.tt/1eddaz0.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include="Id,Codigo,Nombre,TipoDeDatos, Entidad")] Propiedad propiedad)
    {
        if (ModelState.IsValid)
        {
            db.Propiedades.Add(propiedad);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(propiedad);
    }

http://ift.tt/1IM8IYw

http://ift.tt/1JVOxsw

Aucun commentaire:

Enregistrer un commentaire