2

我得到: 从类型“System.String”到类型“System.Web.Mvc.SelectListItem”的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。 将表单发布到控制器时出错,以下是我正在使用的代码:

模型

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

控制器

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();
         } 

看法

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>}

我已经使用 Visual stdio 进行了调试,错误是在导致模型状态失败的性别列表上生成的。任何我弄错的想法,因为我在我的方法中尝试了所有可能的解决方案,但没有任何成功,任何帮助和帮助将不胜感激。

谢谢

4

1 回答 1

1

您的模型属性GenderDisablity提交表单发布时会发生冲突。MVC 尝试将无线电输入的选定值(类型为string[])转换为您的 ViewModel 属性类型IList<SelectListItem> Gender

重命名选定的值或添加新属性

模型属性

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

看法

将您的输入重命名为“SelectedGender”和“SelectedDisablity”

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

行动

也在动作参数中

  public ActionResult HandleFormSubmit(string[] selectedGender, string[] selectedDisability, FormViewModel model){ //...
于 2013-11-12T09:27:12.157 回答