Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在razor中获取作为模型的空异常_C#_Asp.net_.net_Asp.net Mvc_Razor - Fatal编程技术网

C# 在razor中获取作为模型的空异常

C# 在razor中获取作为模型的空异常,c#,asp.net,.net,asp.net-mvc,razor,C#,Asp.net,.net,Asp.net Mvc,Razor,我的asp.netmvc4应用程序中有一个错误视图,如下所示: @model System.Web.Mvc.HandleErrorInfo @{ ViewBag.Title = "Erreur"; } <p>Take it easy</p> <hgroup class="title"> <h1 class="error">Erreur = @Model.Exception</h1> <h1 class="

我的
asp.net
mvc4应用程序中有一个错误视图,如下所示:

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Erreur";
}
<p>Take it easy</p>
<hgroup class="title">
    <h1 class="error">Erreur = @Model.Exception</h1>
    <h1 class="error">Controller = @Model.ControllerName</h1>
    <h1 class="error">Name = @Model.ActionName</h1>
    <h2 class="error">Une erreur s'est produite lors du traitement de la requête.</h2>    
</hgroup>

问题是
模型
始终为空。原因是什么?

您甚至没有向视图提供错误对象

    public ActionResult Error()
    {



        return View();
    }

您甚至没有向视图提供错误对象

    public ActionResult Error()
    {



        return View();
    }

可以按实例将模型对象的实例传递给View()方法

public ActionResult Error()
{
    ErrorViewModel vm=new ErrorViewModel();
    vm.prop1="This is the error message";

    return View(vm);
}

可以按实例将模型对象的实例传递给View()方法

public ActionResult Error()
{
    ErrorViewModel vm=new ErrorViewModel();
    vm.prop1="This is the error message";

    return View(vm);
}

其实这是个好问题。您实际上不需要将相关模型传递给视图。您需要做的是确保设置了适当的过滤器。在MVC 4中,您可以在global.asax.cs中执行此操作:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
       filters.Add(new HandleErrorAttribute());
    }
确保在应用程序启动时调用此函数:

    protected void Application_Start()
    {       
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
    }

现在您的HandleErrorInfo模型将被填充并自动传递到错误视图。

实际上这是一个好问题。您实际上不需要将相关模型传递给视图。您需要做的是确保设置了适当的过滤器。在MVC 4中,您可以在global.asax.cs中执行此操作:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
       filters.Add(new HandleErrorAttribute());
    }
确保在应用程序启动时调用此函数:

    protected void Application_Start()
    {       
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
    }

现在,您的HandleErrorInfo模型将被自动填充并传递到错误视图。

如何显示视图?提供控制器操作代码更具体地说,您的模型在控制器中的何处填充并分配给视图?请提供控制器操作方法代码。或者,您也可以这样做。假设这是
错误
操作,那么您不会将模型传递给视图。您如何期望模型不为空?如何显示视图?提供控制器操作代码更具体地说,您的模型在控制器中的何处填充并分配给视图?请提供控制器操作方法代码。或者,您也可以这样做。假设这是
错误
操作,那么您不会将模型传递给视图。您如何期望模型不为null?您的示例代码是显示修复还是显示问题?您的示例代码是显示修复还是显示问题?