0

我有一个下拉列表,从中填充

视图模型:

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

1 回答 1

1

在对其进行了几次调试并完成之后 - 我注意到它在返回创建时被捕获: return View(email);

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

于 2012-11-18T15:55:35.517 回答