只有在提交表单时出现错误</selectlistitem>时,才会出现'IEnumerable <selectlistitem>'类型的ViewData项目

时间:2012-11-18 01:23:26

标签: asp.net-mvc

我有一个从

填充的下拉列表

视图模型:

public class OrgPages
{
    public int ID { get; set; }
    public string URL { get; set; }
}

助手:

   public SelectList PageList()
    {
        var pagelist = new List<OrgPages>();

        foreach (var count in Enumerable.Range(1, 10))
        {
            var pages = new OrgPages();

            pages.ID = count;

            if(count == 1)
                pages.URL = "Done";
            else if (count == 2)
                pages.URL = "Address";
            else if (count == 3)
                pages.URL = "Import/Export";
            else if (count == 4)
                pages.URL = "Membership";
            else if (count == 5)
                pages.URL = "Business Type";
            else if (count == 6)
                pages.URL = "Industry Type";
            else if (count == 7)
                pages.URL = "Phone Number";
            else if (count == 8)
                pages.URL = "Email";
            else if (count == 9)
                pages.URL = "Website";
            else if (count == 10)
                pages.URL = "Note";
            pagelist.Add(pages);
        }
        return new SelectList(pagelist, "ID", "URL", 0);            
    }

在我的控制器中,我发起了我的帮助:

private DropDownPopulatorController ddp = new DropDownPopulatorController();

在我的创建动作中,而不是帖子,我称之为ViewBag:

ViewBag.OrgPages = ddp.PageList();

在我的HTTP POST创建操作中,我传递了OrgPages值:

[HttpPost]
public ActionResult Create(OrganizationEmail email, int OrgPages)
{
    try
    {
        // TODO: Add insert logic here
        using (var db = new VAGTCEntities())
        {
            db.OrganizationEmails.Add(email);
            db.SaveChanges();
        }
        if (OrgPages == 2)
            return RedirectToAction("Create", "OrgAddress");
        else if (OrgPages == 3)
            return RedirectToAction("Create", "OrgIE");
        else if (OrgPages == 4)
            return RedirectToAction("Create", "OrgMembership");
        else if (OrgPages == 5)
            return RedirectToAction("Create", "OrgBusinessType");
        else if (OrgPages == 6)
            return RedirectToAction("Create", "OrgIndustryType");
        else if (OrgPages == 7)
            return RedirectToAction("Create", "OrgPhone");
        else if (OrgPages == 8)
            return RedirectToAction("Create", "OrgEmail");
        else if (OrgPages == 9)
            return RedirectToAction("Create", "OrgWebsite");
        else if (OrgPages == 10)
            return RedirectToAction("Create", "OrgNote");
        else
            return RedirectToAction("Index");
    }
    catch (Exception)
    {
        ModelState.AddModelError("Error", "This item exists in the database already.");
    }
    return View(email);
}

在我的“创建视图”页面中,我将其放在字段集中:

<div>
    @Html.Label("Add:")
</div>
<div>
    @Html.DropDownList("OrgPages")
</div>

所以,现在,当有人提交表单时,他们可以选择他们很快想要访问的下一页。当提交表单没有错误时(例如没有重复),它可以完美地工作。但是,如果存在重复的主键或类似错误,则会发出错误There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'OrgPages'.

感谢您的帮助!

编辑* 创建行动

public ActionResult Create()
{
    ViewBag.OrgPages = ddp.PageList();
    return View();
}

1 个答案:

答案 0 :(得分:1)

经过多次调试并完成后 - 我注意到它在返回创建时被抓住了:return View(email);

此时它不再具有OrgPages的ViewData。所以,我所做的只是在显示错误后再次在catch块中调用ViewBag.OrgPages = ddp.PageList();。修好了!