Question

I have the following model class:

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


    }

and 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>

and calling editor template

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

i am gettng Model.State is false

with exception error

{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)}

As i am trying to edit edtorTemplate consumed form i was getting error i even try viewModel but nothing make sense at al. since model seems to be binding correctly but on quick watch i found message which says "The parameter conversion from type 'System.String' to type 'OAMS.Models.BASLPAcademics' failed because no type converter can convert between these types." Please share correct solution for this ?

Edit: Fiddler response

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>();

        }
    }

on model state key error is found at,

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

enter image description here

Was it helpful?

Solution

You are trying to add the the entire BASLPApplicationFormModel as a hidden field. Html.HiddenFor<> tries to evaluate the expression as a string, which it wouldn't be able to do for your model, hence the error. Try removing this line from your view:

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

Or is it that you actually want to call this instead?

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

OTHER TIPS

Remove this from your View/EditorTemplate.

@Html.HiddenFor(model => model.BASLPApplication)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top