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; }
}
}
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.
– Felipe Renan
As are your tables, please pass me this. You are sending item worthless this is the error.
– user46523
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]) );
– Felipe Renan
It has error in the generation of its tables the problem this ai.
– user46523
Put all the classes and your dbcontext to help you
– user46523
I’ll put in a new answer, I better edit.
– Felipe Renan