2

我有以下模型类:

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


    }

和编辑器模板

@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.State得到了false

有异常错误

{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,但没有任何意义。因为模型似乎绑定正确,但在快速观看时我发现消息说“从类型'System.String'到类型'OAMS.Models.BASLPAcademics'的参数转换失败,因为没有类型转换器可以在这些类型之间转换。” 请为此分享正确的解决方案?

编辑:提琴手响应

在此处输入图像描述

 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 

在此处输入图像描述

4

2 回答 2

3

您正在尝试将整个添加BASLPApplicationFormModel为隐藏字段。Html.HiddenFor<>尝试将表达式评估为字符串,它无法为您的模型执行此操作,因此出现错误。尝试从您的视图中删除此行:

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

还是您实际上想调用它?

@Html.EditorFor(model => model.BASLPApplication)
于 2013-05-23T12:29:05.137 回答
-1

从您的视图/编辑器模板中删除它。

@Html.HiddenFor(model => model.BASLPApplication)
于 2013-05-24T05:17:47.083 回答