简体   繁体   English

在ViewResult对象中初始化ViewData属性

[英]Initializing ViewData property inside ViewResult object

When I access ViewData inside a method in the controller, I am able to assign the value in the form of dictionary ie. 当我在控制器中的方法内部访问ViewData时,我能够以字典即形式分配值。

ViewData["message"]="this is a custom message";

but I got into a scenario where I was trying to handle the Exception in MVC, here is my code: 但是我遇到了一种尝试在MVC中处理异常的情况,这是我的代码:

 public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && (filterContext.Exception is ArgumentOutOfRangeException))
            {
                filterContext.Result = new ViewResult { ViewName = "Error", ViewData = };
                filterContext.ExceptionHandled = true;
            }
        }

Now when handling exception i would like to pass a message to the error page, so tried to access the Result property of the ExceptionContext. 现在,当处理异常时,我想将一条消息传递给错误页面,因此尝试访问ExceptionContext的Result属性。 Now 现在

my question is why am I not able to assign a value to the ViewData in a dictionary-like a format here 我的问题是为什么我不能以类似字典的格式将值分配给ViewData

filterContext.Result = new ViewResult { ViewName = "Error", ViewData = };

This is also a property returning a ViewDataDictionary object, when am I able to assign a value in the Controller method like this ViewData["message"] = "argument exception error"; 当我能够在Controller方法中分配一个值,例如ViewData [“ message”] =“ argument exception error”时,这也是返回ViewDataDictionary对象的属性 the why am I not able to do the same inside the ViewResult object . 为什么我不能在ViewResult对象中做同样的事情。

I tried it myself and got an understanding on the inner workings of the MVC frameWork, please correct me if I am wrong and please provide an explanation for it, which would make to learn more about the framework. 我自己尝试了一下,并且对MVC框架的内部工作有了了解,如果我错了,请纠正我,并提供一个解释,这将使您更多地了解框架。

When I access ViewData inside a method in the controller, I am able to assign the value in the form of dictionary 当我在控制器的方法中访问ViewData时,我能够以字典的形式分配值

This is because when we call the controller and the method, MVC takes responsibilty to assign objects to all the properties, thats the reason we could assign value for the ViewData inside the method. 这是因为当我们调用控制器和方法时,MVC负责将对象分配给所有属性,这就是我们可以在方法内部为ViewData分配值的原因。

filterContext.Result = new ViewResult { ViewName = "Error", ViewData = }; filterContext.Result = new ViewResult {ViewName =“ Error”,ViewData =};

When we are dealing with the ViewData property of the ViewResult class, we cannot assign the value, in this way ViewData["key"]="some value" because it requires ViewDataDictionary object. 当我们处理ViewResult类的ViewData属性时,我们无法分配该值,因为ViewData [“ key”] =“ some value”需要ViewDataDictionary对象,因此无法通过这种方式进行赋值。 however we can do this to assign the value like this 但是我们可以这样做来分配这样的值

 var d = new ViewDataDictionary();
                d["key"] = "some value";
                filterContext.Result = new ViewResult { ViewName = "Error",ViewData=d };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM