The Parameter Conversion from type 'System.String' to type 'Ramalaguia.Models.Sector' failed because no type converter can Convert between These types

Asked

Viewed 44 times

0

During the realization of a project I came across the following error:

System.Invalidoperationexception: The Parameter Conversion from type 'System.String' to type 'Ramalaguia.Models.Sector' failed because no type converter can Convert between These types.

This is my Controller:

    [HttpGet]
    public ActionResult Create()
    {
        PreparaFormulario();            
        return View();
    }
    [HttpPost]
    public ActionResult Create(RamalModel model)
    {
        if (ModelState.IsValid)
        {
            _db.Ramais.Add(model);
            _db.SaveChanges();
            return RedirectToAction("Index", new { id = model.ID });
        }
        else
        {
            PreparaFormulario();
            return View();
        }

    }    

    [HttpGet]
    public void PreparaFormulario()
    {
        var setores = new List<Setor>();
        using (RamaDb db = new RamaDb())
        {
            setores = db.Setores.ToList();
        }
        ViewBag.ID2 = new SelectList(_db.Setores, "setorID", "setorNome");


    }

A View:

    @Html.DropDownListFor(model => model.setores, (SelectList)ViewBag.ID2)

And the Model:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.Linq;
 using System.Web;

 namespace RamalAguia.Models
 {
 public class RamalModel
 {
    public int ID { get; set; }

    [Required(ErrorMessage = "O nome é obrigatório.")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "O número é obrigatório.")]
    public int Numero { get; set; }



    public Setor setores { get; set; }

How can I fix?

Edit

Obs: i was able to find this error through a breakpoint in Modelstate.Isvalid, when the application will save the past data, within the values I use to save the sector.

Sector Model

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace RamalAguia.Models
 {
    public class Setor
    {
       public int setorID { get; set; }

       public string setorNome { get; set; }

       public Area areas { get; set; }
    }
}

Edit2:

Classes:

Ramalmodel

    public class RamalModel
    {
    public int ID { get; set; }

    [Required(ErrorMessage = "O nome é obrigatório.")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "O número é obrigatório.")]
    public int Numero { get; set; }


    public int SetorId { get; set; }

    [ForeignKey("SetorId")]
    public Setor setores { get; set; }

Sectors

    public class Setor
    {
    public int setorID { get; set; }

    public string setorNome { get; set; }

    public Area areas { get; set; }

And the dbcontext

    namespace RamalAguia.Models
    {
        public class RamaDb : DbContext
        {
             public DbSet<Usuario> Usuarios { get; set; }

             public DbSet<RamalModel> Ramais { get; set; }

             public DbSet<Setor> Setores { get; set; }

             public DbSet<Area> Areas { get; set; }
        }
    }

1 answer

0


Your relationship from the inside is wrong, Select the key of the entity Setor?

It would be more or less that, since it did not put the other class of the relationship:

public class RamalModel
{
    public int ID { get; set; }

    [Required(ErrorMessage = "O nome é obrigatório.")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "O número é obrigatório.")]
    public int Numero { get; set; }

    public int SetorId {get;set;}

    [ForeignKey("SetorId")]
    public Setor setores { get; set; }
}

public class Setor
{
    public int ID { get; set; }

    [Required(ErrorMessage = "O nome é obrigatório.")]
    public string Nome { get; set; }

    [ForeignKey("SetorId")]
    public ICollection<RamalModel> RamalModel {get;set;}
}

In that DropDownList then it would look like this:

@Html.DropDownList("SetorId", (SelectList)ViewBag.ID2)

or do so which is more usual:

In the controller create a Viewbag with the same name as the interface key which is Setorid, follow the model:

ViewBag.SetorId = new SelectList(_db.Setores, "setorID", "setorNome");

and your DropDownList with that model:

@Html.DropDownList("SetorId")
  • Thanks for the answer, but when I update the database it appears in the Package Manager Console: Cannot Insert the value NULL into column 'Setorid', table 'Ramalaguia.Models.Ramadb.dbo.Ramalmodels'; column does not allow nulls. UPDATE fails. The statement has been terminated.

  • As are your tables, please pass me this. You are sending item worthless this is the error.

  • CREATE TABLE [dbo]. [Ramalmodels] ( [ID] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (MAX) NOT NULL, [Number] INT NOT NULL, [setores_setorID] INT NOT NULL, CONSTRAINT [Pk_dbo.Ramalmodels] PRIMARY KEY CLUSTERED ([ID] ASC), CONSTRAINT [Fk_dbo.Ramalmodels_dbo.Setors_setores_setorid] FOREIGN KEY ([setores_setorID]) REFERENCES [dbo]. [Setors] ([setorID]) );

  • It has error in the generation of its tables the problem this ai.

  • Put all the classes and your dbcontext to help you

  • I’ll put in a new answer, I better edit.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.