如何清除应用程序内存中的查看包名称

时间:2015-11-11 12:27:40

标签: c# asp.net-mvc

我将结果集存储在viewdata [“Clientgroup”]中,并将viewdata [“Clientgroup”]分配到下拉列表中,

var title = (from c in db.Titles select c.TitleName).Distinct().ToList();
        var titleList = new List<SelectListItem>();
        foreach (var rel in title)
        {
            var con = new SelectListItem();
            con.Text = rel;
            con.Value = rel;
            titleList.Add(con);
        }
        ViewData["ClientGroup"] = titleList;


 @Html.DropDownListFor(model => model.ClientGroup, (IEnumerable<SelectListItem>)ViewData["ClientGroupItems"], "---Select---", new { @class = "form-control" })

我稍后将viewbag名称更改为:

ViewData["ClientGroupItems"] = titleList;

现在,每当我运行应用程序时,它都会抛出异常:

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

我已经对此做了一些研究,仍然无法解决它,请有人知道如何处理这个问题吗?

发布方法:

[HttpPost]
    public ActionResult Corporate(CorporateViewModel corporate)
    {

        try
        {
            var cust = new Customer()
            {
                ClientType = Session["ClientType"].ToString(),
                ClientId = corporate.ClientId,
                ClientGroup = corporate.ClientGroup,
                OrgName = corporate.OrgName,
                Branch = corporate.Branch,
                RegOffice = corporate.RegOffice,

            };
            db.Customers.Add(cust);
            db.SaveChanges();



            var temCt = Session["clientType"];

            var clientGroup = (from ct in db.ClientTypes
                               join cg in db.ClientGroups
                                   on ct.RowId equals cg.ClientTypeId

                               where ct.TypeName == temCt.ToString()
                               select cg.GroupName).ToList();

            var items = new List<SelectListItem>();

            foreach(var t in clientGroup)
            {
                var s = new SelectListItem();

                s.Text = t;
                s.Value = t;

                items.Add(s);
            }
            ViewData["ClientGroupItems"] = items;

            ModelState.AddModelError("success", "Record created successfully!");
        }
        catch (Exception ex)
        {


            var temCt = Session["clientType"];

            var clientGroup = (from ct in db.ClientTypes
                join cg in db.ClientGroups
                    on ct.RowId equals cg.ClientTypeId

                where ct.TypeName == temCt.ToString()
                select cg.GroupName).ToList();

            var items = new List<SelectListItem>();

            foreach (var t in clientGroup)
            {
                var s = new SelectListItem();

                s.Text = t;
                s.Value = t;

                items.Add(s);
            }
            ViewData["ClientGroupItems"] = items;
            ModelState.AddModelError("", ex.Message);
        }

        return View(corporate);
    }

0 个答案:

没有答案
相关问题