Question

I know it has been asked before on many many occassions but I am a bit confused when this happens to me....

I have a view model like this:

public class RAssessment
    {
        [HiddenInput]
        public string rid { get; set; }

        [HiddenInput]
        public string NextPageAction { get; set; }

        [HiddenInput]
        public string PrevPageAction { get; set; }

        [HiddenInput]
        public string CodeType { get; set; }

        public MultiSelectList CA { get; set; }

        public MultiSelectList NonCA { get; set; }

        public List<SelectListItem> AXM { get; set; }

        public List<SelectListItem> IntOrders { get; set; }

        public List<SelectListItem> IntLang { get; set; }

        public List<SelectListItem> IntLang2 { get; set; }


        //Get RAssessment details
        public RAssessment(int id)
        {
            //get details
        }


        public RAssessment()
        {

        }

}

and a simple get and post controller actions like this:

[HttpGet]
        public ActionResult RAssessment(int id)
        {
            RAssessment ra = new RAssessment(id);            
            return View("RAssessment", ra);            
        }

        [HttpPost]
        public ActionResult RAssessment(RAsessment ra, FormCollection formdata)
        {            
            if (ModelState.IsValid)  
            {
                :
                return RedirectToAction(blah);
            }
            return View();
        }

On my page view.. it has the page declartion:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/MasterPage/Template.Master" Inherits="System.Web.Mvc.ViewPage<xxxx.Models.RAssessment>" %>

When a submit on the view happens, it invokes the actionresult RAssessment but for some reason it gives me the infamous "no parameterless constructor defined for this object". (I know it sounds strange to pass BOTH model and formcollection but I DO need to do it in this case). The normal answer for this case would be "because you have not create a constructor for RAssessment in your model" but as you can see in my model I have done it already and when I put a breakdown inside the empty model constructor the actionresult DOES go there. If I remove the RAssessment parameter inside the actionresult it does work but I do want to pass the model data in the actionresult and then perform some complex model validations. Can anyone show me the right direction? Thanks. WML

Was it helpful?

Solution

I think you would get rid of the error if MultiSelectList would have parameterless constructor or you would add [Bind(Exclude="NonCA, CA")] above your view model. Then you would get those properties null when binding happens in the action marked with [HttpPost].

The reason for all this is that when you have HttpPost it tries to re-create RAssessment ra and all properties/fields inside it with values provided with hidden fields, text fields etc. Unfortunately currently it's not able to re-create NonCA and/or CA.

If you check MSDN you can see that MultiSelectList doesn't have parameterless constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top