0

I´m having a problem creating a DropDownListFor using a relation between to tables that are related on an entity code first:

public class Motivo_Agenda 
{
    [Key]
    [Required]      
    public string DESC_MOTIVO { get; set; }
}

public class Agenda // Agenda da produção
{
    [Key]
    public int ID_AGD { get; set; }
    [Required]       
    public DateTime DATA_AGD { get; set; }       
    [Display(Name = "De")]
    public TimeSpan DE { get; set; }
    [Required]
    [Display(Name = "Até")]
    public TimeSpan ATE { get; set; }
    [Required]       
    public virtual Motivo_Agenda MOTIVO_AGENDA { get; set; }
}

I´m tryng to create a Drop Down List to add a new item to the Agenda Table this is my controller:

bAxaltaModel dbAxalta = new dbAxaltaModel();
    // GET: Agenda
    public ActionResult Edit()
    {
        ViewBag.MOTIVO_AGENDA = new SelectList(dbAxalta.Motivos_Agenda, "DESC_MOTIVO", "DESC_MOTIVO");
        return View();
    }

    [HttpPost]
    public ActionResult Edit([Bind(Include = "DE,ATE,DATA_AGD,FLAG_SAB_DOM,MOTIVO_AGENDA")]Agenda Agendas)
    {

        var errors = ModelState.Values.SelectMany(v => v.Errors).ToList();
        if (ModelState.IsValid != false)
        {                
            dbAxalta.Agendas.Add(Agendas);
            dbAxalta.SaveChanges();
            var query = dbAxalta.Agendas.Where(m => m.DATA_AGD == Agendas.DATA_AGD);
            TempData["Agendas"] = query.ToList();
            return View();
        }
        ViewBag.MOTIVO_AGENDA = new SelectList(dbAxalta.Motivos_Agenda, "DESC_MOTIVO", "DESC_MOTIVO", Agendas.MOTIVO_AGENDA);
        return View();
    }

And my DropDown is this:

 @Html.DropDownListFor(m => m.MOTIVO_AGENDA, ViewBag.MOTIVO_AGENDA as SelectList, htmlAttributes: new { @class = "form-control" })

But I´m getting the following error:

The parameter conversion from type 'System.String' to type 'Axalta_Project.Models.Motivo_Agenda' failed because no type converter can convert between these types.

Any ideas how can I bind these?

  • Why do u have a `dot` in this line `m => m.MOTIVO_AGENDA.` (after `MOTIVO_AGENDA`)? – Sybren Nov 30 '15 at 11:23
  • It was a Typo. But in the code it´s fine. Thanks for leeting me know. – Marcus Vinicius Bianchi Santos Nov 30 '15 at 12:37
  • You cannot bind a `` only posts back a single value). It needs to be `@Html.DropDownListFor(m => m.MOTIVO_AGENDA.DESC_MOTIVO, ViewBag.MOTIVO_AGENDA as SelectList, ... )`. And change the name of the `ViewBag` property so it is not the same as one of your model properties. –  Dec 01 '15 at 00:55
  • When a I do "m.MOTIVO_AGENDA.DESC_MOTIVO" it says that I´m tryng to insert a new "Motivo_Agenda" with the same value, so I´m violating the primary key. It´s he is tryng to insert a new value in the table instead of creating a new relation. – Marcus Vinicius Bianchi Santos Dec 01 '15 at 10:41
  • Then your `Agenda ` model needs a navigation property (the FK for `Motivo_Agenda`) and you bind to that property. –  Dec 01 '15 at 21:44

1 Answers1

0

When using DropDownListFor, the first parameter is the selected value so you will need to add this to your model.

Ideally, use a Model class rather than ViewBag in a similar format to the following:

 public class MyModel {
public int SelectedAgendaId {get; set;}
public SelectList Agendas {get;set;}
}

Then use something like this:

@Html.DropDownListFor(m => m.SelectedAgendaId, Model.Agendas, htmlAttributes: new { @class = "form-control" })
Bluetonic
  • 16
  • 1