Problem with radiobutton Asp mvc

Asked

Viewed 104 times

0

Good evening ,I am trying popular radio button varis as the value contained in my database ,however I am not able to do.

Here’s the view with the radiobutton

@model GuialetoLMS.Models.GuialetoModel
@foreach (var choice in Model.Vestibular)
{
    @Html.RadioButton("answer", @choice.idVestibular ) @choice.NomeVestibular
}

And my controller

  public ActionResult PaginaQuestao()
    {
        return View(db.Vestibular.ToList());
    }
  • What is the error, what is happening?

  • The model item passed into the Dictionary is of type 'System.Collections.Generic.List`1[Guialetolms.Models.Vestibular]', but this Dictionary requires a model item of type 'Guialetolms.Models.Guialetomodel'.

  • The guy you’re passing to the view is wrong db.Vestibular.ToList() is returning a GuialetoLMS.Models.Vestib‌​ular and not a @model GuialetoLMS.Models.GuialetoModel.

1 answer

0

Basically your view is waiting for a Viewmodel GuialetoLMS.Models.GuialetoModel and you’re passing one on to her a different kind,GuialetoLMS.Models.Vestib‌​ular.

If you wanted to pass only the Vestibular list, just change the statement in your view:

@model GuialetoLMS.Models.Vestib‌​ular
@foreach (var choice in Model)
{
    @Html.RadioButton("answer", @choice.idVestibular ) @choice.NomeVestibular
}

Even though you have only included a small snippet of your code I deduce that the first solution will not solve your problem as you should wish to pass other data that are contained in GuialetoModel, then you should create an instance of that object and popular with the content you want.

public ActionResult PaginaQuestao()
{
    var viewModel = new GuialetoLMS.Models.GuialetoModel();

    //... resto do seu código

    viewModel.Vestibular = db.Vestibular.ToList();

    return View(viewModel);
}

Browser other questions tagged

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