asp.net MVC - MVC 5 下拉列表模型状态从类型 'System.String' 到类型 'Namespace.Models.Entity' 的错误转换



我非常新的MVC和我开始理解和开发一个RL(简单)应用程序,但我卡在创建一个记录,我似乎不能张贴我的表单内容,因为这个错误:

"从类型'System '的参数转换。字符串'到类型"Namespace.Models。实体"因为没有转换器可以进行此转换"

在我的一个字段上的

,然而,有另一个带下拉列表的字段也正确工作,也许是因为其中一个字段是模型中的导航关系?该模型用于问卷应用程序下面是主要的Question模型:(先用EF数据库生成)

    public partial class QuestionContent
{
    public QuestionContent()
    {
        this.QuestionTranslations = new HashSet<QuestionTranslations>();
        this.QuestionMedicalCategory = new HashSet<QuestionMedicalCategory>();
        this.QuestionCategory = new HashSet<QuestionCategory>();
        this.QuestionContent1 = new HashSet<QuestionContent>();
    }
    public int QItemId { get; set; }
    public string Language { get; set; }
    public string Content { get; set; }
    public Nullable<int> ParentQId { get; set; }
    public virtual ICollection<QuestionTranslations> QuestionTranslations {       get; set; }
    public virtual ICollection<QuestionMedicalCategory>      QuestionMedicalCategory { get; set; }
    public virtual ICollection<QuestionCategory> QuestionCategory { get; set; }
    public virtual ICollection<QuestionContent> QuestionContent1 { get; set; }
    public virtual QuestionContent QuestionContent2 { get; set; }
}

分类模型是这样的:

namespace AskusOnlinePrototype.Models{
using System;
using System.Collections.Generic;
public partial class QuestionCategory
{
    public QuestionCategory()
    {
        this.QuestionContent = new HashSet<QuestionContent>();
    }
    public int CategoryId { get; set; }
    public string CategoryDescription { get; set; }
    public virtual ICollection<QuestionContent> QuestionContent { get; set; }
}}

我的"创建"控制器(我复制了下拉列表的实现,看到ParentQId Viewbag SelectList在modelstate中不产生任何错误)

public ActionResult Create()
    {
        ViewBag.QuestionCategory = new SelectList(db.QuestionCategory, "CategoryId", "CategoryDescription");
ViewBag.ParentQId = new SelectList(db.QuestionContent, "QItemId", "Content");//Generated by VS
        ViewBag.DefaultLanguage = "EN";
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Language,Content,ParentQId,QuestionMedicalCategory,QuestionCategory")] QuestionContent questionContent)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.QuestionContent.Add(questionContent);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (DataException /* dex */)
        {
            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
        }
        ViewBag.QuestionCategory = new SelectList(db.QuestionCategory, "CategoryId", "CategoryDescription");
        ViewBag.ParentQId = new SelectList(db.QuestionContent, "QItemId", "Content");
            ViewBag.DefaultLanguage = "EN";
            return View(questionContent);

    }

和My Create View(简化以显示问题)

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
     @Html.LabelFor(model => model.QuestionCategory,"Item Category") 
@Html.DropDownListFor(m => m.QuestionCategory,null, "Select Category")

//This DropDownListFor implementation does not generate errors in modelstate        
@Html.LabelFor(model => model.ParentQId, "Select Parent ")
            @Html.DropDownListFor(m=>m.ParentQId, null," -- None --")

        </div>
    </div>

我已经在SO中研究了几个问题,并且已经解决了这个问题几天了,但是我放弃了,因为他们中的大多数处理更复杂的模型,或multiSelectLists,我的意思是,这应该很简单!我的问题是,我知道DefaultModelBinder看到这些字段(因为medicalCategory给了我同样的问题,但我认为解决这个问题也会修复medicalCategory,因为在模型状态下,它显示"预期值"例如在给定选择的Questioncategory中为"1",所以,当DropDownLists绑定到导航关系时,我应该做些什么,是否需要修改modelbinder?如果是这样,你能帮我从哪里开始了解表单后的工作与这些类型的意见?我发现的最类似的问题是[MVC模型状态验证失败的列表框,但答案似乎过于拉伸我的简单模型,尽管如此,如果你认为这是正确的答案,请指出我。

谢谢,如果你不知道答案,但有一个想法,我应该研究我也很感激!

我相信你提到的另一个问题是完全正确的。您需要为您的复杂类型构建转换器,或者执行如下操作

@Html.DropDownListFor(m => m.QuestionCategory.CategoryId,null, "Select Category")

对于任何可能偶然遇到这个问题的人来说,似乎这个问题,如上所述,对应于一个类似的问题,来自这样一个事实,即SelectList中的Select项包含一个值数组,在大多数情况下,这些值不能像在模型中那样转换为复杂类型,在我的情况下,是一个Icollection到另一个模型。这是我对异常和模型以及提交值的绑定的性质的理解,在这种情况下,需要创建类型转换器,然而在某些情况下,我发现更建议构建自定义模型绑定器,因为SelectList中的值甚至不指向模型中的值,它们来自viewbag列表。在我的情况下,我做了一个自定义模型绑定,我使用这些值与foreach,将它们添加到icolllection<>,如果你需要一些帮助开始使用自定义模型绑定,我给你这个链接在MVC中创建自定义模型绑定

最新更新