Question

I am getting : The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types. error when posting a form to controller , below is the code i am using :

Model

 public IList<SelectListItem> Gender { get; set; }
 public IList<SelectListItem> Disablity { get; set; }

Controller

public ActionResult Index()
        {
            FormViewModel objStudentModel = new FormViewModel();

            List<SelectListItem> genderNames = new List<SelectListItem>();
            genderNames.Add(new SelectListItem { Text = "Male", Value = "1" });
            genderNames.Add(new SelectListItem { Text = "Female", Value = "2" });
            genderNames.Add(new SelectListItem { Text = "Prefer not to say", Value = "3" });

            objStudentModel.Gender = genderNames1;
            objStudentModel.Disablity = genderNames1;
            return PartialView("ApplicationForm", objStudentModel);

        }

 [HttpPost]
        public ActionResult HandleFormSubmit(string[] gender, string[] disability, FormViewModel model)
        {
            //model not valid, do not save, but return current umbraco page
           if (ModelState.IsValid == false) // this always comes false 
           {
                return CurrentUmbracoPage();
           }
           string form = "Values + Environment.NewLine;"
           if (gender != null)
            { form += "Gender: " + gender[0] + Environment.NewLine; }
            else
            { form += "Gender: " + "Prefer not to say" + Environment.NewLine; }
            if (disability != null)
            { form += "Disability: " + disability[0] + Environment.NewLine; }
            else
            { form += "Disability: " + "Prefer not to say" + Environment.NewLine; }
            return RedirectToCurrentUmbracoPage();
         } 

View

using (Html.BeginUmbracoForm<FormSurfaceController>("HandleFormSubmit"))
    {

        //var errors = ModelState.Values.SelectMany(v => v.Errors);

        @Html.ValidationSummary();
<div class="col-12 col-sm-12 col-lg-12 ">
                  <label>Are you:</label>
              @foreach (var names1 in @Model.Gender)
              {
                  var tdId = "rd" + names1.Value;
               <input type="radio" id="@tdId" class="chkclass" value="@names1.Text" name="gender" style="padding-right:5px;"/>
                            @names1.Text
                      }

                </div>

            </div>

 <div class="col-12 col-sm-12 col-lg-12 ">
                  @foreach (var names in @Model.Disablity)
                {

                var tdId = "rd" + names.Value;
                   <input type="radio" id="@names.Value" class="chkclass" value="@names.Text" name="disability" style="padding-right:5px;"/>
                    @names.Text
                  }

                </div>}

I have debugged using Visual stdio , the error is generating on the Gender list which is failing the modelstate. Any ideas where I am getting it wrong , coz i have tried all the possible solutions within my approach without any success, Any assistance and help will be appreciated .

Thanks

Was it helpful?

Solution

Your model properties Gender and Disablity would conflicts when submit form post. MVC try to convert radio inputs selected values (which is of type string[]) to your ViewModel property type IList<SelectListItem> Gender

rename the selected values or add new properties

Model properties

public IList<SelectListItem> Gender { get; set; }
public IList<SelectListItem> Disablity { get; set; }
public string[] SelectedGender { get; set; }
public string[] SelectedDisablity { get; set; }

View

rename your input to "SelectedGender" and "SelectedDisablity"

@foreach (var names1 in @ViewBag.Gender)
{
    var tdId = "rd" + names1.Value;
    <input type="radio" class="chkclass" value="@names1.Text" name="SelectedGender" />
    @names1.Text
}

Action

also in action parameters

  public ActionResult HandleFormSubmit(string[] selectedGender, string[] selectedDisability, FormViewModel model){ //...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top