【问题标题】:The ViewData item that has the key is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'具有键的 ViewData 项的类型为“System.Int32”,但必须为“IEnumerable<SelectListItem>”类型
【发布时间】:2014-04-17 18:46:39
【问题描述】:

这是我的模型类:

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

这是我的控制器代码:

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

这是我的视图代码:

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

我在DropDownList:The ViewData item that has the key 'CustID' is of type 'System.Int32' but must be of type 'IEnumerable&lt;SelectListItem&gt;' 上收到此错误。谁能帮帮我?

【问题讨论】:

  • 你能告诉我们你的观点的第一行吗?
  • @model isparx.Models.EstimateModel @{ ViewBag.Title = "Create";布局 = "~/Views/Shared/_Layout.cshtml"; }

  • 您缺少一些代码块,但您的错误来源很清楚:您正在尝试使用整数属性实例化 DropDownList,但帮助程序需要一个可枚举的 @987654328 集合@。基本上,您需要将所有CustIDs 放在一个列表中。
  • 想一想。 CustIDint。您要求系统显示一个下拉列表 - 但是知道该列表中应该出现哪些选项意味着什么?也许切换到 DropDownList 的不同重载,除了告诉它这是用于哪个字段之外,您还提供了它应该显示的明确项目列表。
  • 请同时发布您的GET 控制器。您的(当前)问题出在视图的初始化阶段,而不是发布结果中。

标签: c# asp.net-mvc razor ienumerable


【解决方案1】:

您必须将列表传递给下拉列表,但在这里您传递的是 CustID,即整数。这会导致错误。

试试下面的代码:

1) 使用您的项目创建一个列表。

@{
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) 将新创建的列表传递给以列表为参数的视图。

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

希望这会对你有所帮助..!

编辑:

您可以使用以下示例从数据库创建动态列表。

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

【讨论】:

  • 我如何获取动态列表.. 因为我必须从数据库中获取 CustID.. 这是静态列表...
  • @Pratik :我在编辑中添加了示例。您可以利用给定的示例生成动态列表。
  • 我的错误已被删除...我已将重定向链接提供给另一个页面..感谢您提供动态列表示例..它将帮助我...
  • 你在哪里得到错误。我在代码中的任何地方都看不到 isdetected。
  • 在 linq 条件下 c => IsDetected == false
猜你喜欢
  • 2014-10-02
  • 2012-01-25
  • 1970-01-01
  • 2013-10-22
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多