Click here to Skip to main content
15,436,928 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I have two tables, Class and ClassFee related on ClassID field, and I want when Class is selected in a Dropdownlist of Classes, then ClassFee should be retrieved and displayed.

For this I created a method in controller.

HTML
[HttpGet]
     public PartialViewResult GetClassFee(int? Classid)
     {
         ClassFeeVM CFee = new ClassFeeVM();
         if (Classid != null)
         {
            List<ClassFee> classfees = db.ClassFees.Where(p => p.tblClass.ClassID == Classid).ToList();
             foreach (var item in classfees)
             {
                 CFee.ClassFee = item.ClassFee1;
                 CFee.ClassID = item.ClassID;
                 CFee.ClassName = item.tblClass.ClassName;
                 CFee.FeeType = item.tblFeeType.FeeType;
             }
         //   ViewBag.ClassFees = CFee;
         }
         return PartialView("_ClassFee", CFee);
     }


The error is that instead of values it display as is

The model item passed into the dictionary is of type 'SchoolMVC.Models.ViewModel.ClassFeeVM', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SchoolMVC.Models.ViewModel.ClassFeeVM]'.


What I have tried:

My JQuery/Ajax code for the DropdownList change even is:
HTML
$(function () {
               $("#ClassID").on("change", function () {

                   var url = '@Url.Action("GetClassFee", "ClassFees")' + "?Classid=" + $(this).val();

                   $("#ClassFeeDetails").load(url, function (data) {
                       $("#ClassFeeDetails").html(data);
                   });
               });
           });


And my Partial View is:

HTML
@model IEnumerable<SchoolMVC.Models.ViewModel.ClassFeeVM>

    @foreach (var item in Model)
    {
        <p>@item.ClassFee</p>
        <p>@item.FeeType</p>
        <p>@item.ClassName</p>
    }
Posted
Updated 13-Aug-20 0:35am
v3
Comments
Sandeep Mewara 11-Aug-20 15:34pm    
Seems you forgot to share where are you stuck.

1 solution

The error is pretty clear - your view declares the model to be IEnumerable<ClassFeeVM>, but you are trying to pass in a single ClassFeeVM instance.

Rewrite your code to properly create and populate the list of VMs and pass it to the model:
C#
[HttpGet]
public PartialViewResult GetClassFee(int? Classid)
{
    if (Classid == null) return NotFound();
   
    List<ClassFee> classfees = db.ClassFees.Where(p => p.tblClass.ClassID == Classid).ToList();
    List<ClassFeeVM> model = new List<ClassFeeVM>(classfees.Count);
    foreach (var item in classfees)
    {
        model.Add(new ClassFeeVM
        {
            ClassFee = item.ClassFee1,
            ClassID = item.ClassID,
            ClassName = item.tblClass.ClassName,
            FeeType = item.tblFeeType.FeeType,
        });
    }
    
    return PartialView("_ClassFee", model);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900