MVC错误"是类型'System.Int32'的,但必须是类型为“IEnumerable的< SelectListItem>'。 " [英] MVC Error "is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. "

查看:164
本文介绍了MVC错误"是类型'System.Int32'的,但必须是类型为“IEnumerable的< SelectListItem>'。 "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个模型;

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

这是我的看法
        

This is my view

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

现在,当点击创建按钮,我得到一个错误说

Now when click create button i get an error saying

`具有关键的名字的ViewData的产品类型System.Int32
  但必须是类型为IEnumerable的

`The ViewData item that has the key 'Names' is of type 'System.Int32' but must be of type 'IEnumerable'.

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

I am getting this error is it because ID is int, if yes then how do i convert it?

推荐答案

我个人preFER避免像ViewBag / ViewData的动态的东西尽可能的采取行动的方法和观点之间传输数据。让我们建立一个强类型的视图模型。

I personally prefer to avoid dynamic stuff like ViewBag /ViewData as much as possible to transfer data between action methods and views. Let's build a strongly typed 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>();
   }
}

和在创建操作方法

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

和在你看来,这是强类型我们的视图模型

and in your view, which is strongly typed to our viewmodel

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

}

现在,当你的形式发布,您将获得在视图模型的 MidName 属性所选项目的价值。

Now when your form is posted, You will get the selected item value in the MidName property of the viewmodel.

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

这篇关于MVC错误&QUOT;是类型'System.Int32'的,但必须是类型为“IEnumerable的&LT; SelectListItem&GT;'。 &QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆