我得到: 从类型“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 进行了调试,错误是在导致模型状态失败的性别列表上生成的。任何我弄错的想法,因为我在我的方法中尝试了所有可能的解决方案,但没有任何成功,任何帮助和帮助将不胜感激。
谢谢