【问题标题】:MVC Error "is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. " [duplicate]MVC 错误“属于‘System.Int32’类型,但必须属于‘IEnumerable<SelectListItem>’类型。” [重复]
【发布时间】:2014-06-11 03:55:22
【问题描述】:

我有一个这样的模型;

    public int ID{ get; set; }
    public string MidName{ get; set; }
    public string FirstName{ get; set; }
    public string Surname{ get; set; }

这是我的控制器:

  public ActionResult Create(){
       ViewBag.Names= new SelectList(db.TbName, "ID", "MidName");
       return Viwe();
    }

这是我的看法

        @Html.LabelFor(model => model.Names, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Names", String.Empty)
            @Html.ValidationMessageFor(model => model.Names)
        </div>
    </div>

现在当点击创建按钮时我得到一个错误提示

`具有键“名称”的 ViewData 项的类型为“System.Int32” 但必须是“IEnumerable”类型。

我收到此错误是因为 ID 是 int,如果是,那么我该如何转换它?

【问题讨论】:

  • 收到回发的操作在哪里?
  • 我已经尝试过这个建议,但它对我不起作用!
  • @MikeC。我想我没有。可以举个例子吗?
  • Ehsan 下面有一个例子,虽然你的也需要有参数。有很多关于如何做到这一点的教程,所以我不会费心发布另一个答案。

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


【解决方案1】:

我个人更喜欢尽可能避免像 ViewBag /ViewData 这样的动态东西来在动作方法和视图之间传输数据。让我们构建一个强类型的 Viewmodel。

public class CreateCustomerVM
{
   public string MidName{ get; set; }
   [Required]
   public string FirstName{ get; set; }
   public string Surname{ get; set; }
   public List<SelectListItem> MidNames { set;get;}
   public CreateCustomerVM()
   {
     MidNames=new List<SelectListItem>();
   }
}

在您的 Create 操作方法中

public ActionResult Create()
{
  var vm=new CreateCustomerVM();
  vm.MidNames=GetMidNames();
  return View(vm);
}
private List<SelectListItem> GetMidNames()
{
  return new List<SelectListItem> { 
    new SelectListItem { Value="Mr", Text="Mr"},
    new SelectListItem { Value="Ms", Text="Ms"},
  };
}

在你的视图中,我们的视图模型是强类型的

@model CreateCustomerVM
@using(Html.Beginform())
{

 <div>
   Mid name : @Html.DropdownListFor(s=>s.MidName,Model.MidNames)
   FirstName : @Html.TextBoxFor(s=>s.FirstName)
   <input type="submit" />
 </div>

}

现在,当您的表单发布时,您将在 viewmodel 的 MidName 属性中获得所选项目的值。

[HttpPost]
public ActionResult Create(CreateCustomerVM customer)
{
  if(ModelState.IsValid)
  {
    //read customer.FirstName , customer.MidName 
    // Map it to the properties of your DB entity object
    // and save it to DB
  }
  //Let's reload the MidNames collection again.
  customer.MidNames=GetMidNames();
  return View(customer);
}

【讨论】:

    【解决方案2】:

    在你的视图中使用它:

    @Html.DropDownListFor(x => x.ID, ViewBag.Names, new Dictionary<string, object>{{"class", "control-label col-md-2"}})
    

    应该可以的。

    【讨论】:

    • 我收到一个错误:'System.Web.Mvc.HtmlHelper&lt;Essence.Essence_RunningRecords&gt;' has no applicable method named 'DropDownListFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. 你不见了)所以这就是我用过的@Html.DropDownListFor(x =&gt; x.ID, ViewBag.Names, new Dictionary &lt;string, object&gt;{{"class", "control-label col-md-2"}})
    【解决方案3】:

    在创建的后期操作中再次填充到 viewbag 中:

    public ActionResult Create(){
           ViewBag.Names= new SelectList(db.TbName, "ID", "MidName");
           return View();
        }
    
    
    [HttpPost]
    public ActionResult Create(){
           ViewBag.Names= new SelectList(db.TbName, "ID", "MidName");
           return View();
        }
    

    或尝试使用这样的助手:

    @Html.DropDownListFor(x => x.ID, (SelectList)ViewBag.Names,
               new Dictionary<string, object>{{"class", "control-label col-md-2"}})
    

    【讨论】:

    • 我的代码中确实有一个 httpPost,它看起来就像这样。
    • 你能提供TbName的定义吗??
    • 这在我的 model.Context.cs 文件中是这样的:` public virtual DbSet TbName{ get;放; } `
    • 什么是 inisde 你能告诉编辑帖子的属性
    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 2016-03-25
    • 2016-03-21
    • 2014-11-13
    • 2023-03-12
    相关资源
    最近更新 更多