我有以下模型类:
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