1

When I click on the submit button in the example below, I get an error message like this one:

No parameterless constructor defined for this object.

After getting the error message I am unable to continue using the application. I tried to debug but I don't get any useful information to help in fixing the issue.

Controller:

[MvcApplication.SessionExpire]
    [HttpGet]
    public ActionResult Index()
    {
        DataLinqDB db = new DataLinqDB();

        OpgaverPage model = new OpgaverPage();

        var random = new Random();
        var antalopgaver = db.opgavers.Count();
        var number = random.Next(antalopgaver);

        List<opgaver> List = db.opgavers.Skip(number).Take(1).ToList();
        var A = db.opgavers.Skip(number).Take(1).FirstOrDefault();
        model.Overskift = A.overskift;

        model.OpgaveValueList = new SelectList(
                                new List<SelectListItem>
                                {
                                    new SelectListItem { Selected = true, Text = A.svar1, Value = "A"},
                                    new SelectListItem { Selected = false, Text = A.svar2, Value = "B"},
                                    new SelectListItem { Selected = false, Text = A.svar3, Value = "C"},
                                }, "Value", "Text", 1);

        //angiver id til opgaven
        model.HiddenId = A.Id;

        //Fremvis hvornår man har fået point
        //List<pointantal> PointValue = db.pointantals.Where(x => x.omrade == o && x.brugerid == brugerId).OrderByDescending(i => i.Id).Take(20).ToList();
        //model.ListPoint = PointValue.ToList();

        return View(model);
    }

    //error her
    [MvcApplication.SessionExpire]
    [HttpPost]
    public ActionResult index(OpgaverPage opgavervalue)
    {
        if (ModelState.IsValid)
        {
            DataLinqDB db = new DataLinqDB();
            var opgaver = db.opgavers.FirstOrDefault(i => i.Id == opgavervalue.HiddenId);
            if (opgaver != null)
            {
                if (new SelectList(opgavervalue.OpgaveValueList).SelectedValue.ToString() == "A" 
                    || new SelectList(opgavervalue.OpgaveValueList).SelectedValue.ToString() == "B" 
                    || new SelectList(opgavervalue.OpgaveValueList).SelectedValue.ToString() == "C")
                {
                    Value();
                }
                else
                {
                    //error
                    ModelState.AddModelError("", "Desværre dit svar forkert");
                }
            }
            else
            {
                return RedirectToAction("index", opgavervalue);
            }
        }
        return View();
    }

Model:

public class OpgaverPage
{
    public string Overskift
    {
        get; set;
    }

    public int HiddenId
    {
        get; set;
    }

    [Display(Name ="Spørgsmål")]
    public string Svar
    {
        get; set;
    }

    public SelectList OpgaveValueList
    {
        get; set;
    }

    public List<opgaver> ListPoint
    {
        get; set;
    }
}

View:

@using (Html.BeginForm("index", "spil"))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.HiddenFor(i => i.HiddenId)

    <div class="form-group">
        @Html.LabelFor(x => x.Svar)<br />
        @Html.DropDownListFor(x => x.OpgaveValueList, Model.OpgaveValueList, new
   {
       @class = "form-control"
   })
    </div>


    <button type="submit" class="btn btn-effect-ripple btn-success"><i class="fa fa-check"></i> Tjek mit svar</button>
}

I tried to follow the answer here but I was unable to fix the issue: ASP.NET MVC Dropdown List From SelectList

What is causing this issue?

plr108
  • 1,201
  • 11
  • 16

1 Answers1

0

You cannot bind a <select> to a SelectList. A <select> element post s back a single value. Your model needs a property to bind to, say

public string SelectedOpgaveValue { get; set; }

and then in the view

 @Html.DropDownListFor(x => x.SelectedOpgaveValue, Model.OpgaveValueList, new { @class = "form-control" })

The reason you get the error is that your attempting to bind to a property which is type of SelectList which has no parameterless constructor.

Also you should modify the code for generating the select list to

public IEnumerable<SelectListItem> OpgaveValueList { get; set; }

model.OpgaveValueList = new List<SelectListItem>
{
    new SelectListItem { Text = A.svar1, Value = "A"},
    new SelectListItem { Text = A.svar2, Value = "B"},
    new SelectListItem { Text = A.svar3, Value = "C"},
};

There is no point creating a second select list from the first one. And there is no point setting the Selected property of SelectListItem when your binding to a property because its the value of the property that determines what is selected - if the value of SelectedOpgaveValue is "B", then the second option will be selected irrespective of what values you give the Selected property (the Selected is simply ignored by the HtmlHelper)

  • The problem right now is when I click http post, so when the like must reload the page. then there's no content into the dropdown list. – Jesper Petersen Dec 02 '15 at 07:44
  • That because if you return the view, you need to repopulate the `SelectList` property just as you did in the GET method –  Dec 02 '15 at 07:45
  • And you need to remove all that `if (new SelectList(opgavervalue.OpgaveValueList).SelectedValue.ToString() == "A" ....` nonsense in your POST method. The value of the property `SelectedOpgaveValue` will contain the selected value (either "A", "B", or "C"). –  Dec 02 '15 at 07:51
  • What do you mean _which one is right_? What are you referring to? –  Dec 02 '15 at 08:27
  • If I choose answers, how do I find out. – Jesper Petersen Dec 02 '15 at 08:36
  • What _answers_? Is that what `OpgaveValueList` is - a list of possible answers? As per my answer, you add a property to your model - e.g. `public string SelectedOpgaveValue { get; set; }`and you bind the dropdownlist to it. Then when you post your model, the value of the property `SelectedOpgaveValue` will be either `"A", "B" or "C"` depending on which option the user chose. –  Dec 02 '15 at 08:39