发布包含编辑器模板的编辑器视图时,ModelState为false

时间:2013-05-23 07:21:28

标签: asp.net-mvc asp.net-mvc-4 mvc-editor-templates

我有以下模型类:

 public class BASLPAcademics
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int BASLPAcademicID { get; set; }

        //[UIHint("BASLPApplicationFormModel")]
        public int BASLPApplicationID { get; set; }
        [ForeignKey("BASLPApplicationID")]
        public virtual BASLPApplicationFormModel BASLPApplication { get; set; }

        public string Subject { get; set; }

        public string Physics { get; set; }

        public string Chemistry { get; set; }

        public string Optional { get; set; }

        public int TotalMarks { get; set; }


    }

和Editortemplates

@model OAMS.Models.BASLPAcademics

@{
    ViewBag.Title = "BASLPAcademics";
}

<tr>
    <td>
        @Model.Subject 
        @Html.HiddenFor(model=>model.BASLPAcademicID)
        @Html.HiddenFor(model => model.Subject)
        @Html.HiddenFor(model => model.BASLPApplication)
        @Html.HiddenFor(model => model.BASLPApplicationID)       
    </td>
    <td>
        @Html.EditorFor(model => model.Physics, new { @class = "BASLPAcademics Physics" })
    </td>
    <td>
        @Html.EditorFor(model => model.Chemistry, new { @class = "BASLPAcademics Chemistry" })
    </td>
    <td>
        @Html.EditorFor(model => model.Optional, new { @class = "BASLPAcademics Optional" })
    </td>
    <td>
        @Html.EditorFor(model => model.TotalMarks, new { @class = "BASLPAcademics TotalMarks" })
    </td>    
</tr>

并调用编辑器模板

  @Html.EditorFor(model => model.BaslpAcademic)

我知道Model.Statefalse

有异常错误

{System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'OAMS.Models.BASLPAcademics' failed because no type converter can convert between these types.
   at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
   at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
   at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
   at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)}

因为我正在尝试编辑edtorTemplate消耗的形式我得到错误我甚至尝试viewModel但没有任何意义的al。 因为模型似乎正确绑定但是在快速观察时我发现消息“从类型'System.String'到'OAMS.Models.BASLPAcademics'类型的参数转换失败,因为没有类型转换器可以在这些类型之间转换。”请分享正确的解决方案吗?

编辑: 提琴手回应

enter image description here

 public class BASLPApplicationFormModel
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int BASLPApplicationID { get; set; }

        public string BASLPApplicationNo { get; set; }

        [Required]
        [Display(Name = "Name of Applicant")]
        public string tName { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Date of Birth", Prompt = "DD/MM/YYYY")]
        public DateTime DateOfBirth { get; set; }

        public DateTime Created { get; set; }

        public virtual List<BASLPAcademics> BaslpAcademic { get; set; }

        public BASLPApplicationFormModel()
        {
            BaslpAcademic = new List<BASLPAcademics>();

        }
    }

发现模型状态键错误,

BaslpAcademic[0].BASLPApplication 
BaslpAcademic[1].BASLPApplication 

enter image description here

2 个答案:

答案 0 :(得分:3)

您正尝试将整个BASLPApplicationFormModel添加为隐藏字段。 Html.HiddenFor<>尝试将表达式计算为字符串,它不能为您的模型执行,因此会出错。尝试从您的视图中删除此行:

@Html.HiddenFor(model => model.BASLPApplication)

或者你真的想要打电话给它吗?

@Html.EditorFor(model => model.BASLPApplication)

答案 1 :(得分:-1)

从View / EditorTemplate中删除它。

@Html.HiddenFor(model => model.BASLPApplication)