如果ModelState中不存在某个键,我该如何添加它? aspnetmvc1

时间:2023-01-22 16:31:52

I am trying to create a workaround in my controller which handles a bug in ASP.NET MVC v1. The bug occurs if you post a listbox which has nothing selected (http://forums.asp.net/p/1384796/2940954.aspx).

我试图在我的控制器中创建一个解决方法来处理ASP.NET MVC v1中的错误。如果您发布没有选择任何内容的列表框(http://forums.asp.net/p/1384796/2940954.aspx),则会出现此错误。

Quick Explanation: I have a report that accepts two dates from textboxes and one or more selections from a ListBox. Everything works except for validation if the listbox is left with nothing selected.

快速说明:我有一个报表,它接受来自文本框的两个日期和一个ListBox中的一个或多个选择。如果列表框没有选中任何内容,那么除验证外,一切都有效。

When the form posts and reaches my controller, the model contains all items necessary. However, the ModelState does not contain a key/value for the listbox. To resolve, I was hoping something like this would do the trick:

当表单发布并到达我的控制器时,该模型包含所有必需的项目。但是,ModelState不包含列表框的键/值。为了解决这个问题,我希望这样的事情可以解决问题:

if (!ModelState.ContainsKey("TurnTimeReportModel.Criteria.SelectedQueuesList") || ModelState["TurnTimeReportModel.Criteria.SelectedQueuesList"] == null) {
            ModelState.Keys.Add("TurnTimeReportModel.Criteria.SelectedQueuesList");
            ModelState["TurnTimeReportModel.Criteria.SelectedQueuesList"].Equals(new List<string>());
        }

Unfortuantely, this throws the following exception when I try to add the key: System.NotSupportedException: Mutating a key collection derived from a dictionary is not allowed.

不幸的是,当我尝试添加密钥时,会抛出以下异常:System.NotSupportedException:不允许变换从字典派生的密钥集合。

Any ideas?

Thanks in advance!

提前致谢!

2 个解决方案

#1


4  

Use the ModelState.Add method directly:

直接使用ModelState.Add方法:

ModelState.Add("TurnTimeReportModel.Criteria.SelectedQueuesList", 
               new ModelState{ AttemptedValue = new List<string>() } )

#2


2  

I ended up going with the following which has done the trick:

我最终选择了以下功能:

            if (ModelState.ContainsKey("TurnTimeReportModel.Criteria.SelectedQueuesList") && ModelState["TurnTimeReportModel.Criteria.SelectedQueuesList"] == null) {
            ModelState["TurnTimeReportModel.Criteria.SelectedQueuesList"].Value = new ValueProviderResult("", "", CultureInfo.CurrentUICulture);
        } else if (!ModelState.ContainsKey("TurnTimeReportModel.Criteria.SelectedQueuesList")) {
            ModelState.Add("TurnTimeReportModel.Criteria.SelectedQueuesList", new ModelState{Value = new ValueProviderResult("","",CultureInfo.CurrentUICulture)});
        }

#1


4  

Use the ModelState.Add method directly:

直接使用ModelState.Add方法:

ModelState.Add("TurnTimeReportModel.Criteria.SelectedQueuesList", 
               new ModelState{ AttemptedValue = new List<string>() } )

#2


2  

I ended up going with the following which has done the trick:

我最终选择了以下功能:

            if (ModelState.ContainsKey("TurnTimeReportModel.Criteria.SelectedQueuesList") && ModelState["TurnTimeReportModel.Criteria.SelectedQueuesList"] == null) {
            ModelState["TurnTimeReportModel.Criteria.SelectedQueuesList"].Value = new ValueProviderResult("", "", CultureInfo.CurrentUICulture);
        } else if (!ModelState.ContainsKey("TurnTimeReportModel.Criteria.SelectedQueuesList")) {
            ModelState.Add("TurnTimeReportModel.Criteria.SelectedQueuesList", new ModelState{Value = new ValueProviderResult("","",CultureInfo.CurrentUICulture)});
        }