返回视图

时间:2015-07-30 16:28:23

标签: c# asp.net asp.net-mvc entity-framework

当我坐下来工作时,有一天工作正常,退出并退出。我不能为我的生活弄清楚。我得到的错误是:

enter image description here

我可以启动应用程序,但是当我导航到此页面时,它会在尝试显示视图时启动错误。

这是我的控制器

 // GET: Form/Create
    public ActionResult Create()
    {
        List<METHOD> methods = new List<METHOD>();
        List<NATION> nations = new List<NATION>();
        List<LANGUAGE> languages = new List<LANGUAGE>();
        List<string> headers = new List<string>();
        List<string> footers = new List<string>();
        List<string> cssFiles = new List<string>();

        using(var context = new Entities(_connectionString))
        {

            // Get a list of methods to choose from incase they want to change the method
            methods = context.METHODs.ToList();

            // Get list of nations to choose from
            nations = context.NATIONs.OrderBy(x => x.NAME).ToList();

            // Get list of languages 
            languages = context.LANGUAGES.OrderBy(x => x.DESCRIPTION).ToList();

            // Get list of headers that are used today (from the DB...not solution)
            headers = context.WEB_CONTACT_DETAILs.Select(x => x.HDR_LINK).Distinct().ToList();
            headers.Sort();
            // Get list of footers that are used today (from the DB...not solution)
            footers = context.WEB_CONTACT_DETAILs.Select(x => x.FTR_LINK).Distinct().ToList();
            footers.Sort();
            // Get list of headers that are used today (from the DB...not solution)
            cssFiles = context.WEB_CONTACT_DETAILs.Select(x => x.CSS_LINK).Distinct().ToList();
            cssFiles.Sort();
        }
        ViewData["methods"] = methods;
        ViewData["nations"] = nations;
        ViewData["languages"] = languages;
        ViewData["headers"] = headers;
        ViewData["footers"] = footers;
        ViewData["cssFiles"] = cssFiles;

        return View();
    }

我已经完成了所有操作,所有数据都从DbContext中正确加载,列表对象正是我在返回视图之前所期望的。

以下是视图

@using Company.ContactFormAdmin.Web;
@using Company.ContactFormAdmin.Web.Models
@model FormViewModel
@{
    ViewBag.Title = "Edit";
    List<METHOD> methods = (List<METHOD>)ViewData["methods"];
    List<NATION> nations = (List<NATION>)ViewData["nations"];
    List<LANGUAGE> languages = (List<LANGUAGE>)ViewData["languages"];
    List<string> headers = (List<string>)ViewData["headers"];
    List<string> footers = (List<string>)ViewData["footers"];
    List<string> cssFiles = (List<string>)ViewData["cssFiles"]; //<--this is     where the exception is getting thrown.
}


<h2>New Contact Form</h2>
<div class="container"> //Truncated to keep it short...

我已经逐步浏览了上面的每个List对象,每个对象都填充了我从控制器传递的数据。我也尝试将它们全部设置为null,并且错误仍然会被抛到同一个地方。我也试过没有从控制器传回任何东西,它仍然发生。

@{
    ViewBag.Title = "Edit";
    List<METHOD> methods = null;// (List<METHOD>)ViewData["methods"];
    List<NATION> nations = null;// (List<NATION>)ViewData["nations"];
    List<LANGUAGE> languages = null;// (List<LANGUAGE>)ViewData["languages"];
    List<string> headers = null;// (List<string>)ViewData["headers"];
    List<string> footers = null;// (List<string>)ViewData["footers"];
    List<string> cssFiles = null;// (List<string>)ViewData["cssFiles"];
}

我也尝试过清洁和重建无济于事。如果我删除最后一行(“cssFiles”),则会在前一行(“页脚”)上抛出错误。

完整堆栈跟踪

   at ASP._Page_Views_Form_Create_cshtml.Execute() in c:\Projects\ConsumerServices\ContactFormAdmin\ContactFormAdmin_Soln-MVC-EB\ContactFormAdmin.Web\Views\Form\Create.cshtml:line 8
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

任何人都知道如何进一步解决这个问题或者之前已经处理过这个问题了吗?

1 个答案:

答案 0 :(得分:0)

通过消除过程,我能够解决这个问题。我不知道为什么它突然变成了一个问题,但错误实际上是在视图的不同部分,降低了。

我使用的是传统的Html <input>标签,而不是ASP.NET的Html帮助器。通过替换它:

<input class="form-control" id="WEB_PAGE_NAME" name="WEB_PAGE_NAME" value="@Model.WEB_PAGE_NAME">

用这个:

@Html.TextBoxFor(model => model.WEB_PAGE_NAME, new { @class = "form-control" })

我能够让错误消失。我不知道为什么传统的标签工作了一段时间然后停止了。