没有类型'ienumerable< selectlistitem>'的viewdata项目它有关键'dept.departmentid'。 [英] There is no viewdata item of type 'ienumerable<selectlistitem>' that has the key 'dept.departmentid'.

查看:90
本文介绍了没有类型'ienumerable< selectlistitem>'的viewdata项目它有关键'dept.departmentid'。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是MVC新手。



我创建了一个应用程序在mvc。



我在哪里创建了视图,其中我有绑定部门dropdownlistfor并成功绑定了。



但是,在我点击创建表单中的所有值后,当我点击提交按钮时。

i得到了这个错误:

Hi guys,

i'm new to MVC.

I have creating one application in mvc.

Where i have create view, in which i have bind department dropdownlistfor and successfully i had bind.

But, after inserting all the values in the create form, when i clicked submit button.
i got this error:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Dept.DepartmentID'





我尝试了什么:



业务层



What I have tried:

Business layer

namespace BusinessLayer
{
    public class Workshop_BL
    {
        [Key]
        public  int RecID { get; set; }
        public  string AssetNo { get; set; }
        public Departments Dept { get; set; }
        public  string Location { get; set; }
        public  decimal Cost { get; set; }
        public  string Description { get; set; }
        public  DateTime WoDate { get; set; }
        public  string WoNo { get; set; }
        public  DateTime? ExpDate { get; set; }
        public  DateTime InputDate { get; set; }
        public  string WStatus { get; set; }
        public  string Remarks { get; set; }
        public  int? Manpower { get; set; }
        public  int? EstDays { get; set; }
        public  int? EstHours { get; set; }
        public  DateTime?  PartsRecvDate { get; set; }

    }

    public class Departments
    {
        [Key]
        public int DepartmentID { get; set; }
        public string DepratmentName { get; set; }
    }


}





从Sql server获取数据



Fetching Data from Sql server

public IEnumerable<Departments> Dept
{
    get
    {
        List<Departments> deptlist = new List<Departments>();
        DataTable dt = DA.GetData("SP_GETALL_DEPARTMENTS");
        foreach(DataRow dr in dt.Rows)
        {
            Departments dpl = new Departments();
            dpl.DepartmentID = Convert.ToInt32(dr["DepartmentID"].ToString());
            dpl.DepratmentName = dr["Department_Name"].ToString();
            deptlist.Add(dpl);
        }

        return deptlist;
    }
}





控制器



Controller

[HttpGet]
public ActionResult Create()
{
    WorkshopActivity_Bl objWs = new WorkshopActivity_Bl();
    List<Departments> dpl = objWs.Dept.ToList();
    IEnumerable<SelectListItem> items = objWs.Dept.Select(c => new SelectListItem { Value = c.DepartmentID.ToString(), Text = c.DepratmentName });
    ViewBag.Dept = items;
    return View("Create");
}

[HttpPost]
public ActionResult Create(Workshop_BL bl)
{
    if (ModelState.IsValid)
    {
        WorkshopActivity_Bl objwl = new WorkshopActivity_Bl();
        objwl.InsertWorkshopActivity(bl);
        return RedirectToAction("DetailsList");
    }
    return View("Create");
}





创建视图



Create View

        <div class="editor-label">
            @Html.LabelFor(Model => Model.Dept)
</div>
        <div class="editor-field">
                        @Html.DropDownListFor(Model => Model.Dept, (IEnumerable<SelectListItem>)ViewBag.Dept, "Select")
                        @Html.ValidationMessageFor(Model => Model.Dept)
        </div>





我在get方法中没有错误。



但是我的帖子方法出错了。





所以,伙计们,帮助我。





谢谢

推荐答案

嗨阿卜杜勒,



在Post方法中添加Dropdown绑定代码。



Hi Abdul,

Add the Dropdown binding code in Post Method as well.

WorkshopActivity_Bl objWs = new WorkshopActivity_Bl();
    List<Departments> dpl = objWs.Dept.ToList();
    IEnumerable<SelectListItem> items = objWs.Dept.Select(c => new SelectListItem { Value = c.DepartmentID.ToString(), Text = c.DepratmentName });
    ViewBag.Dept = items;





一旦你在post方法中添加这些行,代码应该可以工作。



ViewData和ViewBag只能在单一请求中使用。这些数据将在PostBack中丢失。所以你需要再次设置它。



也改变查看代码。



你不能写模型.Dept.DepatmentID到Model属性名。



和Model.Dept.FKDeptID一样。



这个应该有所帮助。



Once you add these lines in post method the code should work.

ViewData and ViewBag can be used only in the Single Request. The data will be lost in the PostBack. So you need to set it again.

Also Change the View Code.

You cannot write Model.Dept.DepatmentID to the Model Property Name.

Like Model.Dept.FKDeptID.

This Should help.


这篇关于没有类型'ienumerable&lt; selectlistitem&gt;'的viewdata项目它有关键'dept.departmentid'。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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