The ViewData item that has the key is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

StackOverflow https://stackoverflow.com/questions/22371763

質問

This is my model class:

public class EstimateModel:estimate
{
    public string EstimateNo { get; set; }

    //public SelectList Customer { get; set; }
    //[DisplayName("Customer ID :")]
    public int CustID { get; set; }

    [DisplayName("Customer Name :")]
    public string CustFname { get; set; }

    [DisplayName("Company Name :")]
    public string CompanyName { get; set; }

    [DisplayName("Total:")]
    public decimal total { get; set; }

    [DisplayName("Tax1 :")]
    public decimal tax1 { get; set; }

    public decimal tax2 { get; set; }

    public decimal tax3 { get; set; }

    public decimal subtot { get; set; }

    [DisplayName("Discount :")]
    public decimal Discount { get; set; }

    [DisplayName("GrandTotal:")]
    public decimal grandtotal { get; set; }

    public List<estimate> estimates { get; set; }

    public EstimateModel()
    {
        estimates = new List<estimate>();
    }
}

This is my controller code:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(EstimateModel employee)
    {
        //employee.Customer= new SelectList("CustID","CustFName");
        DataTable dt = new DataTable();

        //for (int i = 0; i < employee.estimates.Count; i++)
        //{

        //    total = total + employee.estimates[i].Amount;

        //} ViewBag.Message = total;
        //Skill abc = new Skill();

               var sys = db.EstimateMasters.Create();
               // var user = db.Adils.Create();
               sys.EstimateNo = employee.EstimateNo;

       for(int i=0 ;i<employee.estimates.Count;i++)
       {
           sys.EstimateNo = employee.EstimateNo;
           sys.CustID = employee.CustID;
               sys.ProductName =  employee.estimates[i].ProductName;
               sys.Quantity = employee.estimates[i].Quantity;
               sys.Price = employee.estimates[i].Price;
               sys.Amount = employee.estimates[i].Amount;
               sys.Total=employee.total;
           sys.Tax1=employee.tax1;
           sys.Tax2 = employee.tax2;
           sys.Tax3 = employee.tax3;
           sys.Discount = employee.Discount;
           sys.SubTotal = employee.subtot;
           sys.GrandTotal = employee.grandtotal;

               db.EstimateMasters.Add(sys);
               db.SaveChanges();
       }

This is my view code:

        <div> @Html.LabelFor(m =>m.CustID)
    @Html.DropDownList("CustID", "---Select---")

        </div>
    </div>
    <div>
        @Html.LabelFor(m => m.CustFname)
        @Html.TextBoxFor(m =>m.CustFname)
        @Html.LabelFor(m=>m.CompanyName)
        @Html.TextBoxFor(m =>m.CompanyName)
    </div>

I am getting this error on DropDownList: The ViewData item that has the key 'CustID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. Can anyone help me?

役に立ちましたか?

解決

You have to pass list to dropdown but here you are passing CustID and that is Integer. This is causing error.

Try following code:

1) Create a list with your items.

@{
List<SelectListItem> CustIDlistItems= new List<SelectListItem>();
CustIDlistItems.Add(new SelectListItem
    {
      Text = "text1",
      Value = "value1"
    });
CustIDlistItems.Add(new SelectListItem
    {
        Text = "text2",
        Value = "value2",
        Selected = true
    });
CustIDlistItems.Add(new SelectListItem
    {
        Text = "text3",
        Value = "value3"
    });
}

2) Pass newly created list to view with list as a parameter.

@Html.DropDownListFor(model => model.Yourproperty, CustIDlistItems, "-- Select Status --")

Hope this will help you..!

EDIT :

You can utilize following example for creating dynamic list from database.

public IEnumerable<SelectListItem> GetTrainingSubjectsList(int selectedValue)
        {
            List<SelectListItem> TrainingSubjectsList = new List<SelectListItem>();

            TrainingSubjectsList.Add(new SelectListItem() { Selected = true, Text = "Select Subject", Value = "" });
            var TrainingSubjects = (from subjects in _context.TrainingDetails.Where(c => c.IsDeleted == false)
                                    select subjects).ToList();

            foreach (TrainingDetail TrainingDetail in TrainingSubjects)
            {
                SelectListItem Item = new SelectListItem();

                Item.Text = TrainingDetail.Title;
                Item.Value = TrainingDetail.TrainingDetailId.ToString();

                if (selectedValue == TrainingDetail.TrainingDetailId)
                {
                    Item.Selected = true;
                }

                TrainingSubjectsList.Add(Item);
            }

            return TrainingSubjectsList;
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top